import UrlBuilder from '~/services/connection/urlBuilder' import { QUERY_TYPE } from '~/types/enums' import User from '~/tests/unit/fixture/models/User' import Organization from '~/tests/unit/fixture/models/Organization' import { repositoryHelper } from '~/services/store/repository' describe('invoke()', () => { describe('getDefaultUrl()', () => { it('should throw an error if URL is missing', () => { expect(() => UrlBuilder.build({ type: QUERY_TYPE.DEFAULT })).toThrow() }) it('should return the URL concat with Root URL', () => { expect(UrlBuilder.build({ type: QUERY_TYPE.DEFAULT, url: 'users' })).toEqual('/api/users') }) }) describe('getEnumUrl()', () => { it('should throw an error if enumType is missing', () => { expect(() => UrlBuilder.build({ type: QUERY_TYPE.ENUM })).toThrow() }) it('should return the Enum URL concat with Root URL', () => { expect(UrlBuilder.build({ type: QUERY_TYPE.ENUM, enumType: 'billing_type' })).toEqual('/api/enum/billing_type') }) }) describe('getModelUrl()', () => { it('should throw an error if model is missing', () => { expect(() => UrlBuilder.build({ type: QUERY_TYPE.MODEL })).toThrow() }) it('should return the Model URL concat with Root URL', () => { const repositoryHelperMock = repositoryHelper as jest.Mocked repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users') expect(UrlBuilder.build({ type: QUERY_TYPE.MODEL, model: User })).toEqual('/api/users') }) it('should throw an error if rootModel is defined AND rootId is missing', () => { const repositoryHelperMock = repositoryHelper as jest.Mocked repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users') expect(() => UrlBuilder.build({ type: QUERY_TYPE.MODEL, model: User, rootModel: Organization })).toThrow() }) it('should return the Root Model URL, Model Url concat with Root URL', () => { const repositoryHelperMock = repositoryHelper as jest.Mocked repositoryHelperMock.getEntity = jest.fn() .mockReturnValueOnce('users') .mockReturnValueOnce('organizations') expect(UrlBuilder.build({ type: QUERY_TYPE.MODEL, model: User, rootModel: Organization, rootId: 1 })).toEqual('/api/organizations/1/users') }) it('should return a concatenated url from a base and a tail', () => { expect(UrlBuilder.concat('/api/', 'test')).toEqual('/api/test') }) it('should return a concatenated url from a base and a any number of tails', () => { expect(UrlBuilder.concat('/api/', 'test', 'foo', 'bar')).toEqual('/api/test/foo/bar') }) it('the parts of the url shall be properly joined with forward slashes', () => { expect(UrlBuilder.concat('/api', 'test/', '/foo')).toEqual('/api/test/foo') }) it('shall return the base url if no tail is passed', () => { expect(UrlBuilder.concat('/api')).toEqual('/api') }) }) })