| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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";
- let $constructUrl:ConstructUrl
- beforeAll(async ()=>{
- $constructUrl = new ConstructUrl()
- })
- 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<typeof repositoryHelper>;
- 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<typeof repositoryHelper>;
- repositoryHelperMock.getEntity = jest.fn().mockReturnValue('users')
- expect(() => $constructUrl.invoke({
- type:QUERY_TYPE.MODEL,
- model: User,
- root_model: Organization
- })).toThrow()
- })
- it('should return the Root Model URL, Model Url concat with Root URL', ()=>{
- const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>;
- repositoryHelperMock.getEntity = jest.fn()
- .mockReturnValueOnce('users')
- .mockReturnValueOnce('organizations')
- expect($constructUrl.invoke({
- type:QUERY_TYPE.MODEL,
- model: User,
- root_model: Organization,
- root_id:1
- })).toEqual('/api/organizations/1/users')
- })
- })
- })
|