import ConstructUrl from '~/services/connection/constructUrl' 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(() => ConstructUrl.invoke({ type: QUERY_TYPE.DEFAULT })).toThrow() }) it('should return the URL concat with Root URL', () => { expect(ConstructUrl.invoke({ type: QUERY_TYPE.DEFAULT, url: 'users' })).toEqual('/api/users') }) }) describe('getEnumUrl()', () => { it('should throw an error if enumType is missing', () => { expect(() => ConstructUrl.invoke({ type: QUERY_TYPE.ENUM })).toThrow() }) it('should return the Enum URL concat with Root URL', () => { expect(ConstructUrl.invoke({ type: QUERY_TYPE.ENUM, enumType: 'billing_type' })).toEqual('/api/enum/billing_type') }) }) describe('getModelUrl()', () => { it('should throw an error if model is missing', () => { expect(() => ConstructUrl.invoke({ 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(ConstructUrl.invoke({ 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(() => ConstructUrl.invoke({ 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(ConstructUrl.invoke({ type: QUERY_TYPE.MODEL, model: User, rootModel: Organization, rootId: 1 })).toEqual('/api/organizations/1/users') }) }) })