Skip to content

Commit

Permalink
658 Mock the documentViewer in unit tests (#795)
Browse files Browse the repository at this point in the history
* unskips the test suite and mocks the doc viewer

* replace expect find by text with and expectation ththe mockDocViewer is called with correct args

* adds working expect statements

* replaces toHaveBeenLastCalledWith with ToHaveBeenCalledWith
  • Loading branch information
pmarsh-scottlogic authored Jan 23, 2024
1 parent a0fcdcb commit 5c62350
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions frontend/src/components/DocumentViewer/DocumentViewBox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DocViewerProps } from '@cyntler/react-doc-viewer/dist/esm/DocViewer';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
Expand All @@ -18,7 +19,7 @@ interface MockDocument {
content: string;
}

describe.skip('DocumentViewBox component tests', () => {
describe('DocumentViewBox component tests', () => {
const URI = 'localhost:1234';
const defaultDocuments: MockDocument[] = [
{
Expand All @@ -45,6 +46,15 @@ describe.skip('DocumentViewBox component tests', () => {
getDocumentMetas: mockGetDocumentMetas,
}));

const mockDocumentViewer = vi.hoisted(() => vi.fn());
vi.mock('@cyntler/react-doc-viewer', () => ({
default: (props: DocViewerProps) => {
mockDocumentViewer(props);
return <div>DocumentViewer</div>;
},
DocViewerRenderers: vi.fn(),
}));

const realFetch = global.fetch;

beforeAll(() => {
Expand All @@ -59,13 +69,15 @@ describe.skip('DocumentViewBox component tests', () => {
global.fetch = realFetch;
});

function getMockedDocumentMetas(documents: MockDocument[]) {
return documents.map((doc) => ({
filename: doc.filename,
uri: `${URI}/${doc.filename}`,
}));
}

function setupMocks(documents: MockDocument[]) {
mockGetDocumentMetas.mockResolvedValue(
documents.map((doc) => ({
filename: doc.filename,
uri: `${URI}/${doc.filename}`,
}))
);
mockGetDocumentMetas.mockResolvedValue(getMockedDocumentMetas(documents));
mockGlobalFetch.mockImplementation(
(uri: string, { method }: RequestInit) => {
if (uri.startsWith(URI)) {
Expand Down Expand Up @@ -130,7 +142,12 @@ describe.skip('DocumentViewBox component tests', () => {
expect(
screen.getByText(`1 out of ${documents.length}`)
).toBeInTheDocument();
expect(await screen.findByText(documents[0].content)).toBeInTheDocument();
expect(mockDocumentViewer).toHaveBeenCalledWith(
expect.objectContaining({
activeDocument: getMockedDocumentMetas(documents)[0],
documents: getMockedDocumentMetas(documents),
})
);
});

test('WHEN the next button is clicked THEN the next document is shown', async () => {
Expand All @@ -146,10 +163,12 @@ describe.skip('DocumentViewBox component tests', () => {
expect(
screen.getByText(`2 out of ${documents.length}`)
).toBeInTheDocument();
/* This is sporadically, non_deterministically failing! See #658 for details
expect(
await screen.findByText(documents[1].content)
).toBeInTheDocument();*/
expect(mockDocumentViewer).toHaveBeenCalledWith(
expect.objectContaining({
activeDocument: getMockedDocumentMetas(documents)[1],
documents: getMockedDocumentMetas(documents),
})
);
});

test('WHEN the previous button is clicked THEN the previous document is shown', async () => {
Expand All @@ -166,10 +185,12 @@ describe.skip('DocumentViewBox component tests', () => {
expect(
screen.getByText(`1 out of ${documents.length}`)
).toBeInTheDocument();
/* This is sporadically, non_deterministically failing! See #658 for details
expect(
await screen.findByText(documents[0].content)
).toBeInTheDocument()*/
expect(mockDocumentViewer).toHaveBeenCalledWith(
expect.objectContaining({
activeDocument: getMockedDocumentMetas(documents)[0],
documents: getMockedDocumentMetas(documents),
})
);
});

test('GIVEN the first document is shown THEN previous button is disabled', async () => {
Expand Down

0 comments on commit 5c62350

Please sign in to comment.