import { formatResponseApi } from '../serializer' import type { ResponseFormatted } from '../types' describe('serializer', () => { const meta = { current_page: 1, next_page: null, per_page: 25, prev_page: null, total_pages: 1, total_count: 3, } const headers = { uid: 'rafaleo@gmail.com', } const exampleResponseObject = { data: { id: '224', email: 'joffrey.berrier@lecollectionist.com', firstname: 'Joffrey', lastname: 'Berrier', }, headers, } const exampleResponseObjectWithAttributes = { data: { id: '224', type: 'user', attributes: { firstname: 'Joffrey', lastname: 'Berrier', email: 'joffrey.berrier@lecollectionist.com', }, }, headers, } const exampleResponseObjectWithRelationship = { data: { id: '2678', type: 'house', attributes: { name: 'Villa Sigala', }, relationships: { firstPhoto: { data: { id: '99305', type: 'photo', }, }, destination: { data: { id: '434', type: 'destination', }, }, }, }, headers, } const exampleResponseWithRelationship = { data: [ { id: '2678', type: 'house', attributes: { name: 'Villa Massira', }, relationships: { firstPhoto: { data: { id: '99305', type: 'photo', }, }, destination: { data: { id: '434', type: 'destination', }, }, }, }, { id: '1553', type: 'house', attributes: { name: 'Villa Joy', }, relationships: { firstPhoto: { data: { id: '44226', type: 'photo', }, }, destination: { data: { id: '305', type: 'destination', }, }, }, }, ], } const exampleResponseWithoutRelationship = { data: [ { id: '2678', type: 'house', attributes: { name: 'Villa Massira', }, }, { id: '1553', type: 'house', attributes: { name: 'Villa Joy', }, }, ], } const exampleResponseWithoutIdOnRelationship = { data: [ { id: '4085', type: 'contract', attributes: { checkInDate: '2020-07-25', checkOutDate: '2020-08-01', clientFirstname: 'David', clientLastname: 'Blanc', clientUploadYousignFileToken: null, destinationName: 'Cap Ferret', houseBathrooms: 1, houseBedrooms: 1, houseCapacity: 2, houseClusterName: {}, houseCountryName: {}, houseName: 'Villa Les Grives', }, relationships: { house: { data: { id: '542', type: 'house', }, }, houseFirstPhoto: { data: null, }, }, }, ], } const exampleResponseWithIncludedAndRelationships = { data: { id: '2812', type: 'house', attributes: { bathrooms: 7, bedrooms: 7, capacity: 16, conciergeServiceOffer: 'club', name: 'Villa Sigala', slug: { en: 'villa-sigala-lourmarin-surroundings', fr: 'villa-sigala-lourmarin-alentours', }, address: '2 Chemin des Huguenots', checkInTime: '16:00', checkOutTime: '11:00', gpslatitude: '43.760969', gpslongitude: '5.254315', oldName: 'Le Tasseou', }, relationships: { firstPhoto: { data: { id: '93515', type: 'photo' } }, contract: { data: { id: '1000', type: 'contract' } }, codes: { data: [ { id: '257', type: 'code' }, { id: '258', type: 'code' }, ], }, stayInformation: { data: { id: '1135', type: 'stayInformation' }, }, houseContacts: { data: [ { id: '131', type: 'houseContact' }, { id: '591', type: 'houseContact' }, { id: '2840', type: 'houseContact' }, ], }, housePlaceDistances: { data: [ { id: '101953', type: 'housePlaceDistance' }, { id: '101916', type: 'housePlaceDistance' }, { id: '102025', type: 'housePlaceDistance' }, { id: '101989', type: 'housePlaceDistance' }, { id: '102097', type: 'housePlaceDistance' }, { id: '102255', type: 'housePlaceDistance' }, { id: '102291', type: 'housePlaceDistance' }, ], }, housePlaces: { data: [ { id: '77621', type: 'housePlace' }, { id: '77784', type: 'housePlace' }, { id: '77067', type: 'housePlace' }, { id: '77159', type: 'housePlace' }, { id: '77513', type: 'housePlace' }, { id: '77406', type: 'housePlace' }, { id: '77477', type: 'housePlace' }, { id: '77549', type: 'housePlace' }, { id: '77712', type: 'housePlace' }, { id: '78101', type: 'housePlace' }, { id: '86705', type: 'housePlace' }, { id: '61877', type: 'housePlace' }, { id: '62616', type: 'housePlace' }, { id: '62903', type: 'housePlace' }, { id: '63190', type: 'housePlace' }, { id: '63764', type: 'housePlace' }, { id: '64051', type: 'housePlace' }, { id: '64338', type: 'housePlace' }, { id: '64625', type: 'housePlace' }, { id: '64912', type: 'housePlace' }, { id: '65199', type: 'housePlace' }, { id: '65499', type: 'housePlace' }, { id: '65742', type: 'housePlace' }, { id: '65985', type: 'housePlace' }, { id: '66472', type: 'housePlace' }, { id: '66759', type: 'housePlace' }, { id: '67033', type: 'housePlace' }, { id: '67651', type: 'housePlace' }, { id: '67982', type: 'housePlace' }, { id: '68269', type: 'housePlace' }, { id: '68556', type: 'housePlace' }, { id: '69417', type: 'housePlace' }, { id: '69704', type: 'housePlace' }, { id: '69991', type: 'housePlace' }, { id: '70278', type: 'housePlace' }, { id: '70565', type: 'housePlace' }, { id: '70852', type: 'housePlace' }, { id: '71139', type: 'housePlace' }, { id: '71426', type: 'housePlace' }, { id: '71713', type: 'housePlace' }, { id: '72287', type: 'housePlace' }, { id: '72905', type: 'housePlace' }, ], }, }, }, included: [ { id: '524', type: 'user', attributes: { address: null, allowPasswordChange: false, bankAccountOwner: null, bic: null, birthdate: null, city: null, civility: 'unspecified', clientGeneralRentalConditionSigned: false, comment: '', country: null, createdAt: '2020-04-09T13:07:05.253+02:00', email: 'kevin.fh.guillot@gmail.com', firstName: 'Kevin', iban: null, invitationSentAt: null, isAdmin: false, lastName: 'Guillot', locale: 'fr', nationality: null, ownerGeneralRentalConditionSigned: true, phone: '+33 6 52 39 29 91', postalCode: null, provider: 'email', termsOfServiceSigned: true, uid: 'kevin.fh.guillot@gmail.com', updatedAt: '2022-03-30T12:31:06.764+02:00', }, }, { id: '131', type: 'houseContact', attributes: { contactableType: 'User', depositResponsible: false, receptionResponsible: false, roles: ['owner'], signatory: true, }, relationships: { house: { data: { id: '2812', type: 'house' } }, contactable: { data: { id: '524', type: 'user' } }, }, }, { id: '152', type: 'adminUser', attributes: { allowPasswordChange: false, comment: null, createdAt: '2019-03-22T12:25:38.452+01:00', email: 'anastasia.feather@lecollectionist.com', firstname: 'Anastasia', hubspotId: null, isActive: true, lastname: 'Feather', phone: '0785400010', provider: 'email', restricted: false, uid: 'anastasia.feather@lecollectionist.com', updatedAt: '2022-03-31T11:31:50.062+02:00', }, }, { id: '591', type: 'houseContact', attributes: { contactableType: 'AdminUser', depositResponsible: false, receptionResponsible: true, roles: ['local_ops'], signatory: false, }, relationships: { house: { data: { id: '2812', type: 'house' } }, contactable: { data: { id: '152', type: 'adminUser' } }, }, }, { id: '173', type: 'houseStaff', attributes: { comment: "S'occupe de la villa + Chef", email: 'jmk.nomad@gmail.com', firstName: 'Jean-Marc', lastName: 'Krikorian', locale: 'fr', phone: '06 12 38 20 60', }, }, { id: '2840', type: 'houseContact', attributes: { contactableType: 'HouseStaff', depositResponsible: false, receptionResponsible: false, roles: ['household_manager', 'handyman', 'house_keeper', 'other'], signatory: false, }, relationships: { house: { data: { id: '2812', type: 'house' } }, contactable: { data: { id: '173', type: 'houseStaff' } }, }, }, { id: '1000', type: 'contract', attributes: { additionalHousekeepingHouseFee: '28.0', }, meta: { currentBalance: 11682, seasonalPurchase: false, }, }, ], } describe('Response api with an object', () => { it('has data.attributes', () => { expect(formatResponseApi(exampleResponseObjectWithAttributes)).toEqual({ data: { id: '224', email: 'joffrey.berrier@lecollectionist.com', firstname: 'Joffrey', lastname: 'Berrier', }, headers, }) }) it('has data.attributes && data.relationship', () => { expect(formatResponseApi(exampleResponseObjectWithRelationship)).toEqual({ data: { destinationId: '434', firstPhotoId: '99305', id: '2678', name: 'Villa Sigala', }, headers, }) }) it("hasn't data.attributes", () => { expect(formatResponseApi(exampleResponseObject)).toEqual({ data: { id: '224', email: 'joffrey.berrier@lecollectionist.com', firstname: 'Joffrey', lastname: 'Berrier', }, headers, }) }) }) describe('Response api with an array', () => { it('has data.relationships', () => { expect( formatResponseApi({ ...exampleResponseWithRelationship, meta, headers, }), ).toEqual({ data: [ { firstPhotoId: '99305', id: '2678', destinationId: '434', name: 'Villa Massira', }, { firstPhotoId: '44226', id: '1553', destinationId: '305', name: 'Villa Joy', }, ], meta, headers, }) }) it('hasnt data.relationships', () => { expect( formatResponseApi({ ...exampleResponseWithoutRelationship, meta, }), ).toEqual({ data: [ { id: '2678', name: 'Villa Massira', }, { id: '1553', name: 'Villa Joy', }, ], meta, }) }) it('hasnt id on data.relationships', () => { expect( formatResponseApi({ ...exampleResponseWithoutIdOnRelationship, meta, }), ).toEqual({ data: [ { id: '4085', houseId: '542', houseFirstPhotoId: null, checkInDate: '2020-07-25', checkOutDate: '2020-08-01', clientFirstname: 'David', clientLastname: 'Blanc', clientUploadYousignFileToken: null, destinationName: 'Cap Ferret', houseBathrooms: 1, houseBedrooms: 1, houseCapacity: 2, houseClusterName: {}, houseCountryName: {}, houseName: 'Villa Les Grives', }, ], meta, }) }) it('has included and relationships', () => { const res = formatResponseApi({ ...exampleResponseWithIncludedAndRelationships, meta, }) as ResponseFormatted expect(res.data).toEqual({ id: '2812', bathrooms: 7, bedrooms: 7, capacity: 16, conciergeServiceOffer: 'club', contractId: '1000', name: 'Villa Sigala', slug: { en: 'villa-sigala-lourmarin-surroundings', fr: 'villa-sigala-lourmarin-alentours', }, address: '2 Chemin des Huguenots', checkInTime: '16:00', checkOutTime: '11:00', gpslatitude: '43.760969', gpslongitude: '5.254315', oldName: 'Le Tasseou', firstPhotoId: '93515', stayInformationId: '1135', housePlaceDistancesIds: [ '101953', '101916', '102025', '101989', '102097', '102255', '102291', ], houseContactsIds: ['131', '591', '2840'], codesIds: ['257', '258'], housePlacesIds: [ '77621', '77784', '77067', '77159', '77513', '77406', '77477', '77549', '77712', '78101', '86705', '61877', '62616', '62903', '63190', '63764', '64051', '64338', '64625', '64912', '65199', '65499', '65742', '65985', '66472', '66759', '67033', '67651', '67982', '68269', '68556', '69417', '69704', '69991', '70278', '70565', '70852', '71139', '71426', '71713', '72287', '72905', ], }) expect(res.included?.users).toEqual({ id: '524', type: 'user', address: null, allowPasswordChange: false, bankAccountOwner: null, bic: null, birthdate: null, city: null, civility: 'unspecified', clientGeneralRentalConditionSigned: false, comment: '', country: null, createdAt: '2020-04-09T13:07:05.253+02:00', email: 'kevin.fh.guillot@gmail.com', firstName: 'Kevin', iban: null, invitationSentAt: null, isAdmin: false, lastName: 'Guillot', locale: 'fr', nationality: null, ownerGeneralRentalConditionSigned: true, phone: '+33 6 52 39 29 91', postalCode: null, provider: 'email', termsOfServiceSigned: true, uid: 'kevin.fh.guillot@gmail.com', updatedAt: '2022-03-30T12:31:06.764+02:00', }) expect(res.included?.houseContacts).toEqual([ { contactableId: '524', houseId: '2812', contactableType: 'User', depositResponsible: false, id: '131', receptionResponsible: false, roles: ['owner'], signatory: true, type: 'houseContact', }, { contactableId: '152', houseId: '2812', contactableType: 'AdminUser', depositResponsible: false, id: '591', receptionResponsible: true, roles: ['local_ops'], signatory: false, type: 'houseContact', }, { contactableId: '173', houseId: '2812', contactableType: 'HouseStaff', depositResponsible: false, id: '2840', receptionResponsible: false, roles: ['household_manager', 'handyman', 'house_keeper', 'other'], signatory: false, type: 'houseContact', }, ]) expect(res.included?.adminUsers).toEqual({ id: '152', type: 'adminUser', allowPasswordChange: false, comment: null, createdAt: '2019-03-22T12:25:38.452+01:00', email: 'anastasia.feather@lecollectionist.com', firstname: 'Anastasia', hubspotId: null, isActive: true, lastname: 'Feather', phone: '0785400010', provider: 'email', restricted: false, uid: 'anastasia.feather@lecollectionist.com', updatedAt: '2022-03-31T11:31:50.062+02:00', }) expect(res.included?.houseStaffs).toEqual({ id: '173', type: 'houseStaff', comment: "S'occupe de la villa + Chef", email: 'jmk.nomad@gmail.com', firstName: 'Jean-Marc', lastName: 'Krikorian', locale: 'fr', phone: '06 12 38 20 60', }) expect(res.included?.contracts).toEqual({ additionalHousekeepingHouseFee: '28.0', id: '1000', meta: { currentBalance: 11682, seasonalPurchase: false, }, type: 'contract', }) }) }) })