import { deleteRecordInPagesData, updateAllRecordsInPagesData, updateOrCreateRecordInPagesData, updateRecordInPagesData, } from '../../../utils' const data = { pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'message 1' }, { id: '2', type: 'Message', text: 'message 2' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'message 3' }, { id: '4', type: 'Message', text: 'message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], } describe('updateAllRecordsInPagesData', () => { it('should update all records in the pages data', () => { const result = updateAllRecordsInPagesData({ data, processRecord: r => ({ ...r, text: 'updated ' + r.text }), }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'updated message 1' }, { id: '2', type: 'Message', text: 'updated message 2' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'updated message 3' }, { id: '4', type: 'Message', text: 'updated message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) }) describe('updateOrCreateRecordInPagesData', () => { it('should update the record in the pages data', () => { const id = '3' const record = createRecord({ id }) const result = updateOrCreateRecordInPagesData({ data, record }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'message 1' }, { id: '2', type: 'Message', text: 'message 2' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'example 3' }, { id: '4', type: 'Message', text: 'message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) it('should update the record in the pages data with a custom processRecord function', () => { const id = '2' const record = createRecord({ id }) const result = updateOrCreateRecordInPagesData({ data, record, processRecord: r => ({ ...r, text: 'updated ' + r.text }), }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'message 1' }, { id, type: 'Message', text: `updated example ${id}` }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'message 3' }, { id: '4', type: 'Message', text: 'message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) }) describe('updateOrCreateRecordInPagesData function', () => { it('should update the record in the pages data', () => { const id = '3' const record = createRecord({ id }) const result = updateOrCreateRecordInPagesData({ data, record }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'message 1' }, { id: '2', type: 'Message', text: 'message 2' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'example 3' }, { id: '4', type: 'Message', text: 'message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) it('should update the record in the pages data with a custom processRecord function', () => { const id = '2' const record = createRecord({ id }) const result = updateOrCreateRecordInPagesData({ data, record, processRecord: r => ({ ...r, text: 'updated ' + r.text }), }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'message 1' }, { id, type: 'Message', text: `updated example ${id}` }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'message 3' }, { id: '4', type: 'Message', text: 'message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) it('should add records that do not yet exist, but does not run processRecord', () => { const id = '5' const record = createRecord({ id }) const result = updateOrCreateRecordInPagesData({ data, record, processRecord: r => ({ ...r, text: 'updated ' + r.text }), }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id, type: 'Message', text: `updated example ${id}` }, { id: '1', type: 'Message', text: 'message 1' }, { id: '2', type: 'Message', text: 'message 2' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [ { id: '3', type: 'Message', text: 'message 3' }, { id: '4', type: 'Message', text: 'message 4' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) it('should skip the record if it does not exist', () => { const id = '5' const record = createRecord({ id }) const result = updateRecordInPagesData({ data, record, processRecord: r => ({ ...r, text: 'updated ' + r.text }), }) expect(result).toEqual(data) }) }) describe('deleteRecordInPagesData', () => { it('should delete records from page', () => { const id = '4' const record = createRecord({ id }) const result = deleteRecordInPagesData({ data, record, }) expect(result).toEqual({ pageParams: {}, pages: [ { data: [ { id: '1', type: 'Message', text: 'message 1' }, { id: '2', type: 'Message', text: 'message 2' }, ], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, { data: [{ id: '3', type: 'Message', text: 'message 3' }], included: [], links: {}, meta: { count: 2, totalCount: 2 }, }, ], }) }) }) const createRecord = ({ id }: { id: string }) => ({ id, text: `example ${id}`, type: 'Message', })