Skip to content

Commit

Permalink
Assert requests are as expected when clicking on items. Remove 2s sle…
Browse files Browse the repository at this point in the history
…ep that don't need (added to try and debug test).
  • Loading branch information
jg210 committed Feb 14, 2024
1 parent 2eda080 commit c6edc9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- unit test cache expiry
- might need to reduce below polling interval to make this possible?
- unit test polling
- https://mswjs.io/docs/recipes/polling/
- unit test changing Table localAuthorityId
- would have found data/currentData bug
- should go back to loading... state
Expand Down
39 changes: 24 additions & 15 deletions src/frontend/src/__tests__/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BASE_PATHNAME, Establishments, LocalAuthorities, LocalAuthority } from
import { setupServer } from 'msw/node';
import { http, HttpResponse } from 'msw';
import { RenderWithStore, serverURL } from './util';
import { escape, escapeRegExp, flatMap, last } from 'lodash';

Check failure on line 8 in src/frontend/src/__tests__/App.test.tsx

View workflow job for this annotation

GitHub Actions / build

'escape' is defined but never used

function checkBoilerplate() {
// banner
Expand Down Expand Up @@ -172,23 +173,31 @@ describe("App", () => {

// Clicking on an authority

const establishmentResponseRecords = () => responseRecords.filter(responseRecord => {
// Filters out just establishment json requests, then parses the local authority id.
const establishmentRequestLocalAuthorityIds = () => flatMap(responseRecords, (responseRecord => {
const url = new URL(responseRecord.request.url);
const isEstablishmentUrl = url.pathname.startsWith(`${BASE_PATHNAME}/localAuthority/`);
return isEstablishmentUrl;
});

expect(establishmentResponseRecords()).toHaveLength(0);

await selectLocalAuthority(localAuthorities[0].localAuthorityId, user);
await new Promise(r => setTimeout(r, 2000));
expect(establishmentResponseRecords()).toHaveLength(1);

await selectLocalAuthority(localAuthorities[1].localAuthorityId, user);
expect(establishmentResponseRecords()).toHaveLength(2);

const pattern = escapeRegExp(`${BASE_PATHNAME}/localAuthority/`) + '([0-9]+)';
const match = url.pathname.match(pattern);
return match ? [ match[1] ] : [];
}));

expect(establishmentRequestLocalAuthorityIds()).toHaveLength(0);

// Click on one item.
const localAuthorityId0 = localAuthorities[0].localAuthorityId;
await selectLocalAuthority(localAuthorityId0, user);
expect(establishmentRequestLocalAuthorityIds()).toHaveLength(1);
expect(last(establishmentRequestLocalAuthorityIds())).toEqual(localAuthorityId0.toString());

// Click on another item.
const localAuthorityId1 = localAuthorities[1].localAuthorityId;
await selectLocalAuthority(localAuthorityId1, user);
expect(establishmentRequestLocalAuthorityIds()).toHaveLength(2);
expect(last(establishmentRequestLocalAuthorityIds())).toEqual(localAuthorityId1.toString());

// Clicking on first item again.
await selectLocalAuthority(localAuthorities[1].localAuthorityId, user);
expect(establishmentResponseRecords()).toHaveLength(2);
expect(establishmentRequestLocalAuthorityIds()).toHaveLength(2); // cached by RTK query.

});

Expand Down

0 comments on commit c6edc9f

Please sign in to comment.