urlBuilder.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import UrlBuilder from '~/services/connection/urlBuilder'
  2. import { QUERY_TYPE } from '~/types/enums'
  3. import User from '~/tests/unit/fixture/models/User'
  4. import Organization from '~/tests/unit/fixture/models/Organization'
  5. import { repositoryHelper } from '~/services/store/repository'
  6. import {DataProviderArgs} from "~/types/interfaces";
  7. describe('invoke()', () => {
  8. describe('getDefaultUrl()', () => {
  9. it('should throw an error if URL is missing', () => {
  10. expect(() => UrlBuilder.build({
  11. type: QUERY_TYPE.DEFAULT
  12. })).toThrow()
  13. })
  14. it('should return the URL concat with Root URL', () => {
  15. expect(UrlBuilder.build({
  16. type: QUERY_TYPE.DEFAULT,
  17. url: 'users'
  18. })).toEqual('/users')
  19. })
  20. })
  21. describe('getEnumUrl()', () => {
  22. it('should throw an error if enumType is missing', () => {
  23. expect(() => UrlBuilder.build({
  24. type: QUERY_TYPE.ENUM
  25. })).toThrow()
  26. })
  27. it('should return the Enum URL concat with Root URL', () => {
  28. expect(UrlBuilder.build({
  29. type: QUERY_TYPE.ENUM,
  30. enumType: 'billing_type'
  31. })).toEqual('/api/enum/billing_type')
  32. })
  33. })
  34. describe('getModelUrl()', () => {
  35. it('should throw an error if model is missing', () => {
  36. expect(() => UrlBuilder.build({
  37. type: QUERY_TYPE.MODEL
  38. })).toThrow()
  39. })
  40. it('should return the Model URL concat with Root URL', () => {
  41. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  42. repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
  43. expect(UrlBuilder.build({
  44. type: QUERY_TYPE.MODEL,
  45. model: User
  46. })).toEqual('/api/users')
  47. })
  48. it('should throw an error if rootModel is defined AND rootId is missing', () => {
  49. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  50. repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
  51. expect(() => UrlBuilder.build({
  52. type: QUERY_TYPE.MODEL,
  53. model: User,
  54. rootModel: Organization
  55. })).toThrow()
  56. })
  57. it('should return the Root Model URL, Model Url concat with Root URL', () => {
  58. const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
  59. repositoryHelperMock.getEntity = jest.fn()
  60. .mockReturnValueOnce('users')
  61. .mockReturnValueOnce('organizations')
  62. expect(UrlBuilder.build({
  63. type: QUERY_TYPE.MODEL,
  64. model: User,
  65. rootModel: Organization,
  66. rootId: 1
  67. })).toEqual('/api/organizations/1/users')
  68. })
  69. it('should return a concatenated url from a base and a tail', () => {
  70. expect(UrlBuilder.concat('/api/', 'test')).toEqual('/api/test')
  71. })
  72. it('should return a concatenated url from a base and a any number of tails', () => {
  73. expect(UrlBuilder.concat('/api/', 'test', 'foo', 'bar')).toEqual('/api/test/foo/bar')
  74. })
  75. it('the parts of the url shall be properly joined with forward slashes', () => {
  76. expect(UrlBuilder.concat('/api', 'test/', '/foo')).toEqual('/api/test/foo')
  77. })
  78. it('shall return the base url if no tail is passed', () => {
  79. expect(UrlBuilder.concat('/api')).toEqual('/api')
  80. })
  81. })
  82. describe('getImageUrl()', () => {
  83. it('should throw an error if imgArgs is missing', () => {
  84. expect(() => UrlBuilder.build({
  85. type: QUERY_TYPE.IMAGE
  86. })).toThrow()
  87. })
  88. it('should return the File download URL concat with Root URL', () => {
  89. const args: DataProviderArgs = {
  90. type: QUERY_TYPE.IMAGE,
  91. imgArgs: {
  92. id: 1,
  93. height: 50,
  94. width: 50
  95. }
  96. }
  97. jest.useFakeTimers('modern');
  98. jest.setSystemTime(new Date(2020, 3, 1));
  99. expect(UrlBuilder.build(args)).toEqual('/api/files/1/download/50x50?1585692000000')
  100. jest.useRealTimers();
  101. })
  102. })
  103. describe('getFileUrl()', () => {
  104. it('should return the URL concat with Root URL and base URL', () => {
  105. expect(UrlBuilder.build({
  106. type: QUERY_TYPE.FILE,
  107. baseUrl: 'http://local.opentalent.fr'
  108. })).toEqual('http://local.opentalent.fr/api/files')
  109. })
  110. })
  111. })