model-name-to-model-key.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {expect} from 'chai';
  2. import {modelNameToModelKey} from './model-name-to-model-key.js';
  3. describe('modelNameToModelKey', function () {
  4. it('should return a simple lowercase string as is', function () {
  5. expect(modelNameToModelKey('user')).to.be.eq('user');
  6. });
  7. it('should convert to lowercase and remove hyphens and underscores', function () {
  8. const modelNames = [
  9. 'userProfileDetails',
  10. 'UserProfileDetails',
  11. 'user-profile-details',
  12. 'user_profile_details',
  13. 'User-Profile-Details',
  14. 'User_Profile_Details',
  15. 'USER-PROFILE-DETAILS',
  16. 'USER_PROFILE_DETAILS',
  17. 'USERPROFILEDETAILS',
  18. 'userprofiledetails',
  19. ];
  20. modelNames.forEach(v =>
  21. expect(modelNameToModelKey(v)).to.be.eq('userprofiledetails'),
  22. );
  23. });
  24. it('should handle a mixed string with uppercase, hyphens and underscores', function () {
  25. const modelName = 'User_Profile-Details';
  26. const expected = 'userprofiledetails';
  27. expect(modelNameToModelKey(modelName)).to.be.eq(expected);
  28. });
  29. it('should not remove numbers from the string', function () {
  30. const modelName = 'Type1-Model_2';
  31. const expected = 'type1model2';
  32. expect(modelNameToModelKey(modelName)).to.be.eq(expected);
  33. });
  34. it('should remove the "model" word from a model name', function () {
  35. const modelNames = [
  36. 'userProfileDetailsModel',
  37. 'UserProfileDetailsModel',
  38. 'user-profile-details-model',
  39. 'user_profile_details_model',
  40. 'User-Profile-Details-Model',
  41. 'User_Profile_Details_Model',
  42. 'USER-PROFILE-DETAILS-MODEL',
  43. 'USER_PROFILE_DETAILS_MODEL',
  44. ];
  45. modelNames.forEach(v =>
  46. expect(modelNameToModelKey(v)).to.be.eq('userprofiledetails'),
  47. );
  48. });
  49. it('should not remove the "model" suffix as a part of last word in a model name', function () {
  50. const exceptions = ['SUPERMODEL', 'supermodel'];
  51. exceptions.forEach(v =>
  52. expect(modelNameToModelKey(v)).to.be.eq('supermodel'),
  53. );
  54. });
  55. it('should throw an error for an empty string', function () {
  56. const throwable = () => modelNameToModelKey('');
  57. expect(throwable).to.throw(
  58. 'The model name should be a non-empty String ' +
  59. 'without spaces, but "" was given.',
  60. );
  61. });
  62. it('should throw an error for a string with spaces', function () {
  63. const throwable = () => modelNameToModelKey('user profile');
  64. expect(throwable).to.throw(
  65. 'The model name should be a non-empty String ' +
  66. 'without spaces, but "user profile" was given.',
  67. );
  68. });
  69. it('should throw an error for null', function () {
  70. const throwable = () => modelNameToModelKey(null);
  71. expect(throwable).to.throw(
  72. 'The model name should be a non-empty String ' +
  73. 'without spaces, but null was given.',
  74. );
  75. });
  76. it('should throw an error for undefined', function () {
  77. const throwable = () => modelNameToModelKey(undefined);
  78. expect(throwable).to.throw(
  79. 'The model name should be a non-empty String ' +
  80. 'without spaces, but undefined was given.',
  81. );
  82. });
  83. it('should throw an error for a number', function () {
  84. const throwable = () => modelNameToModelKey(123);
  85. expect(throwable).to.throw(
  86. 'The model name should be a non-empty String ' +
  87. 'without spaces, but 123 was given.',
  88. );
  89. });
  90. it('should throw an error for an object', function () {
  91. const throwable = () => modelNameToModelKey({name: 'test'});
  92. expect(throwable).to.throw(
  93. 'The model name should be a non-empty String ' +
  94. 'without spaces, but Object was given.',
  95. );
  96. });
  97. it('should throw an error for an array', function () {
  98. const throwable = () => modelNameToModelKey(['test']);
  99. expect(throwable).to.throw(
  100. 'The model name should be a non-empty String ' +
  101. 'without spaces, but Array was given.',
  102. );
  103. });
  104. });