import { endpointMaker } from '../endpointMaker' import type { FilterType } from '../filterFormatter' describe('endpointMaker', () => { test('should add includes in endpoint', () => { //Given const includes = { include: ['house'], fields: { contract: ['currency'], }, } //When const endpoint = `v3/user/houses${endpointMaker({ includes })}` //Then expect(endpoint).toBe( 'v3/user/houses?include=house&fields[contract]=currency&page=1&per=25', ) }) test('should remove ? if not have includes', () => { //Given const includes = undefined //When const endpoint = `v3/user/houses${endpointMaker({ includes })}` //Then expect(endpoint).toBe('v3/user/houses?page=1&per=25') }) test('should add filters in endpoint', () => { //Given const filter: FilterType = { fields: { check_out_date: { operation: 'lessThan', value: '2022-10-10', }, booker_type: { operation: 'equals', value: 'Contract' }, }, sort: '-check_in_date', } //When const endpoint = `v3/user/houses${endpointMaker({ filter })}` //Then expect(endpoint).toBe( 'v3/user/houses?filter[check_out_date]=lessThan:2022-10-10&filter[booker_type]=equals:Contract&sort=-check_in_date&page=1&per=25', ) }) test('should remove ? if not have filter', () => { //Given const filter = undefined //When const endpoint = `v3/user/houses${endpointMaker({ filter })}` //Then expect(endpoint).toBe('v3/user/houses?page=1&per=25') }) test('should add includes and filters and custom pagination in endpoint', () => { //Given const includes = { include: ['house'], fields: { contract: ['currency'], }, } const filter: FilterType = { fields: { check_out_date: { operation: 'lessThan', value: '2022-10-10', }, booker_type: { operation: 'equals', value: 'Contract' }, }, sort: '-check_in_date', } const pagination = { per: '10', page: '2' } //When const endpoint = `v3/user/houses${endpointMaker({ includes, filter, pagination, })}` //Then expect(endpoint).toBe( 'v3/user/houses?include=house&fields[contract]=currency&filter[check_out_date]=lessThan:2022-10-10&filter[booker_type]=equals:Contract&sort=-check_in_date&page=2&per=10', ) }) test('should add only custom pagination in endpoint', () => { //Given const pagination = { per: '10', page: '2' } //When const endpoint = `v3/user/houses${endpointMaker({ pagination, })}` //Then expect(endpoint).toBe('v3/user/houses?page=2&per=10') }) })