import { LOGOUT } from '../auth' import reducer from '../data' jest.mock('../../utils/user-locale', () => ({ getUserLocale: () => 'en-US', })) describe('ducks/data', () => { describe('reducer', () => { test('CREATE_OBJECT_PENDING', () => { const state = reducer(undefined, { type: 'CREATE_OBJECT_PENDING' }) expect(state.collections).toEqual({}) expect(state.pagination).toEqual({}) expect(state.tables).toEqual({}) expect(state.errors).toEqual({}) }) test('FETCH_OBJECT_FULFILLED indexes the returned object', () => { const tableId = 't_84452056cf3341cab599143e749071b8' const entityId = 37 const action = { type: 'FETCH_OBJECT_FULFILLED', payload: { data: { id: entityId, c_5535febac4c748ab8e214d59eb79ee30: 23, c_ec3483a363a644b6985dc8c4063ff51f: 'Read', created_at: '2022-01-05T18:35:29.529Z', updated_at: '2022-01-05T18:35:29.529Z', uuid: '41b6de69-df02-4a85-99e8-7d719b7f046e', _type: tableId, }, }, meta: { appId: 'babb3f1c-9d9b-4719-8a4f-acabad50f4cd', datasourceId: '7bqr6whe15ufgcdj9b17qflju', tableId, datasource: { type: 'apto-backend', }, isCurrentUser: false, }, } const state = reducer(undefined, action) expect(state.tables[tableId][entityId].uuid).toBe( '41b6de69-df02-4a85-99e8-7d719b7f046e' ) expect( state.tables[tableId][entityId].c_5535febac4c748ab8e214d59eb79ee30 ).toBe(23) expect( state.tables[tableId][entityId].c_ec3483a363a644b6985dc8c4063ff51f ).toBe('Read') }) test('LOGOUT clears the indexes', () => { const fetchFulfilledAction = { type: 'FETCH_OBJECT_FULFILLED', payload: { data: { id: 37, c_5535febac4c748ab8e214d59eb79ee30: 23, c_ec3483a363a644b6985dc8c4063ff51f: 'Read', created_at: '2022-01-05T18:35:29.529Z', updated_at: '2022-01-05T18:35:29.529Z', uuid: '41b6de69-df02-4a85-99e8-7d719b7f046e', _type: 't_84452056cf3341cab599143e749071b8', }, }, meta: { appId: 'babb3f1c-9d9b-4719-8a4f-acabad50f4cd', datasourceId: '7bqr6whe15ufgcdj9b17qflju', tableId: 't_84452056cf3341cab599143e749071b8', datasource: { type: 'apto-backend', }, isCurrentUser: false, }, } const logoutAction = { type: LOGOUT, } const postFetch = reducer(undefined, fetchFulfilledAction) const postLogout = reducer(postFetch, logoutAction) expect(Object.keys(postLogout.tables).length).toBe(0) }) }) })