Skip to content

Commit

Permalink
Merge pull request #1021 from US-Trustee-Program/CAMS-478-pre-fetch-o…
Browse files Browse the repository at this point in the history
…ffice-staff

CAMS-478 - Prefetch office staff and cache on login
  • Loading branch information
amorrow-flexion authored Nov 8, 2024
2 parents 5ea9eb0 + 532af07 commit 8cae2e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ describe('offices repo', () => {
.mockResolvedValue('inserted-id');

await repo.putOfficeStaff(officeCode, session.user);
expect(replaceOneSpy).toHaveBeenCalledWith(expect.anything(), staff, true);
expect(replaceOneSpy).toHaveBeenCalledWith(
expect.anything(),
{ ...staff, updatedOn: expect.anything() },
true,
);
});

describe('error handling', () => {
Expand Down
13 changes: 12 additions & 1 deletion user-interface/src/login/Session.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { render } from '@testing-library/react';
import { render, waitFor } from '@testing-library/react';
import { BrowserRouter, MemoryRouter } from 'react-router-dom';
import * as reactRouter from 'react-router';
import { CamsSession } from '@common/cams/session';
import LocalStorage from '@/lib/utils/local-storage';
import { LOGIN_PATHS, LOGIN_SUCCESS_PATH } from './login-library';
import { Session, SessionProps } from './Session';
import { MockData } from '@common/cams/test-utilities/mock-data';
import Api2 from '@/lib/models/api2';
import { USTP_OFFICE_DATA_MAP } from '@common/cams/offices';

describe('Session', () => {
const testSession: CamsSession = {
user: {
id: 'mockId',
name: 'Mock User',
offices: [USTP_OFFICE_DATA_MAP.get('USTP_CAMS_Region_2_Office_Manhattan')!],
},
provider: 'mock',
accessToken: MockData.getJwt(),
Expand All @@ -34,6 +37,14 @@ describe('Session', () => {
);
}

test('should prefetch office staff for each division the user is assigned to', () => {
const getOfficeAttorneys = vi.spyOn(Api2, 'getOfficeAttorneys');
renderWithProps();
waitFor(() => {
expect(getOfficeAttorneys).toHaveBeenCalledTimes(testSession.user.offices!.length);
});
});

test('should write the session to local storage', () => {
const setSession = vi.spyOn(LocalStorage, 'setSession');
renderWithProps();
Expand Down
7 changes: 7 additions & 0 deletions user-interface/src/login/Session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ export function useStateAndActions() {
errorMessage: '',
});

function postLoginTasks(session: CamsSession) {
session.user.offices?.forEach((office) => {
Api2.getOfficeAttorneys(office.officeCode);
});
}

function getMe() {
if (state.isLoaded) return;
const newState = { ...state };
Api2.getMe()
.then((response) => {
const session = response.data;
LocalStorage.setSession(session);
postLoginTasks(session);
newState.isLoaded = true;
})
.catch((error) => {
Expand Down

0 comments on commit 8cae2e5

Please sign in to comment.