Skip to content

Commit

Permalink
Merge pull request #44 from apollographql/lh/fix-tests
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
lizhennessy authored Jul 10, 2023
2 parents ddcfeaa + 4805b4b commit 9eb263f
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 24 deletions.
4 changes: 2 additions & 2 deletions client/src/components/__tests__/module-detail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '../../utils/test-utils';
import { renderWithRouter, cleanup } from '../../utils/test-utils';
import ModuleDetail from '../module-detail';

const mockModule = {
Expand Down Expand Up @@ -41,6 +41,6 @@ describe('Module Detail View', () => {
afterEach(cleanup);

it('renders without error', () => {
render(<ModuleDetail module={mockModule} track={mockParentTrack} />);
renderWithRouter(<ModuleDetail module={mockModule} track={mockParentTrack} />);
});
});
4 changes: 2 additions & 2 deletions client/src/components/__tests__/modules-navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '../../utils/test-utils';
import { renderWithRouter, cleanup } from '../../utils/test-utils';
import ModuleNav from '../modules-navigation';

const mockModule = {
Expand Down Expand Up @@ -41,6 +41,6 @@ describe('Modules Navigation View', () => {
afterEach(cleanup);

it('renders without error', () => {
render(<ModuleNav module={mockModule} track={mockParentTrack} />);
renderWithRouter(<ModuleNav module={mockModule} track={mockParentTrack} />);
});
});
4 changes: 2 additions & 2 deletions client/src/components/__tests__/track-detail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '../../utils/test-utils';
import { renderWithRouter, cleanup } from '../../utils/test-utils';
import TrackDetail from '../track-detail';

const mockTrack = {
Expand Down Expand Up @@ -32,6 +32,6 @@ describe('Module Detail View', () => {
afterEach(cleanup);

it('renders without error', () => {
render(<TrackDetail {...mockTrack} />);
renderWithRouter(<TrackDetail {...mockTrack} />);
});
});
4 changes: 2 additions & 2 deletions client/src/containers/__tests__/track-card.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { renderApollo, cleanup, waitForElement } from '../../utils/test-utils';
import { renderApolloWithRouter, cleanup, waitForElement } from '../../utils/test-utils';
import TrackCard from '../track-card';

const mockTrackCardData = {
Expand All @@ -21,7 +21,7 @@ describe('Track Card', () => {

it('renders track Card', async () => {
const mocks = [];
const { getByText } = await renderApollo(
const { getByText } = await renderApolloWithRouter(
<TrackCard track={mockTrackCardData} />,
{
mocks,
Expand Down
25 changes: 21 additions & 4 deletions client/src/pages/__tests__/tracks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import React from 'react';
// this adds custom jest matchers from jest-dom
import '@testing-library/jest-dom/extend-expect';
import { InMemoryCache } from '@apollo/client';
import { renderApollo, cleanup, waitForElement } from '../../utils/test-utils';
import Tracks, { TRACKS } from '../tracks';
import { InMemoryCache, gql } from '@apollo/client';
import { renderApolloWithRouter, cleanup, waitForElement } from '../../utils/test-utils';
import Tracks from '../tracks';

/** Best practice is to export this operation from the component file. We've defined it separately to remain consistent with the course content. */
const TRACKS = gql`
query getTracks {
tracksForHome {
id
title
thumbnail
length
modulesCount
author {
name
photo
}
}
}
`;

const mockTrack = {
id: 'c_0',
Expand Down Expand Up @@ -35,7 +52,7 @@ describe('Tracks Page', () => {
},
];

const { getByText } = await renderApollo(<Tracks />, {
const { getByText } = await renderApolloWithRouter(<Tracks />, {
mocks,
cache,
});
Expand Down
9 changes: 9 additions & 0 deletions client/src/utils/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { render } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom'
import '@testing-library/jest-dom/extend-expect';
import { MockedProvider } from '@apollo/client/testing';

Expand All @@ -22,5 +23,13 @@ const renderApollo = (
);
};

export const renderWithRouterGenerator = (renderer) => (node, options) => {
return renderer(<BrowserRouter>{node}</BrowserRouter>, options)
}

export const renderWithRouter = renderWithRouterGenerator(render)

export const renderApolloWithRouter = renderWithRouterGenerator(renderApollo)

export * from '@testing-library/react';
export { renderApollo };
4 changes: 2 additions & 2 deletions final/client/src/components/__tests__/module-detail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '../../utils/test-utils';
import { renderWithRouter, cleanup } from '../../utils/test-utils';
import ModuleDetail from '../module-detail';

const mockModule = {
Expand Down Expand Up @@ -41,6 +41,6 @@ describe('Module Detail View', () => {
afterEach(cleanup);

it('renders without error', () => {
render(<ModuleDetail module={mockModule} track={mockParentTrack} />);
renderWithRouter(<ModuleDetail module={mockModule} track={mockParentTrack} />);
});
});
4 changes: 2 additions & 2 deletions final/client/src/components/__tests__/modules-navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '../../utils/test-utils';
import { renderWithRouter, cleanup } from '../../utils/test-utils';
import ModuleNav from '../modules-navigation';

const mockModule = {
Expand Down Expand Up @@ -41,6 +41,6 @@ describe('Modules Navigation View', () => {
afterEach(cleanup);

it('renders without error', () => {
render(<ModuleNav module={mockModule} track={mockParentTrack} />);
renderWithRouter(<ModuleNav module={mockModule} track={mockParentTrack} />);
});
});
4 changes: 2 additions & 2 deletions final/client/src/components/__tests__/track-detail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, cleanup } from '../../utils/test-utils';
import { renderWithRouter, cleanup } from '../../utils/test-utils';
import TrackDetail from '../track-detail';

const mockTrack = {
Expand Down Expand Up @@ -32,6 +32,6 @@ describe('Module Detail View', () => {
afterEach(cleanup);

it('renders without error', () => {
render(<TrackDetail {...mockTrack} />);
renderWithRouter(<TrackDetail {...mockTrack} />);
});
});
4 changes: 2 additions & 2 deletions final/client/src/containers/__tests__/track-card.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { renderApollo, cleanup, waitForElement } from '../../utils/test-utils';
import { renderApolloWithRouter, cleanup, waitForElement } from '../../utils/test-utils';
import TrackCard from '../track-card';

const mockTrackCardData = {
Expand All @@ -21,7 +21,7 @@ describe('Track Card', () => {

it('renders track Card', async () => {
const mocks = [];
const { getByText } = await renderApollo(
const { getByText } = await renderApolloWithRouter(
<TrackCard track={mockTrackCardData} />,
{
mocks,
Expand Down
25 changes: 21 additions & 4 deletions final/client/src/pages/__tests__/tracks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import React from 'react';
// this adds custom jest matchers from jest-dom
import '@testing-library/jest-dom/extend-expect';
import { InMemoryCache } from '@apollo/client';
import { renderApollo, cleanup, waitForElement } from '../../utils/test-utils';
import Tracks, { TRACKS } from '../tracks';
import { InMemoryCache, gql } from '@apollo/client';
import { renderApolloWithRouter, cleanup, waitForElement } from '../../utils/test-utils';
import Tracks from '../tracks';

/** Best practice is to export this operation from the component file. We've defined it separately to remain consistent with the course content. */
const TRACKS = gql`
query getTracks {
tracksForHome {
id
title
thumbnail
length
modulesCount
author {
name
photo
}
}
}
`;

const mockTrack = {
id: 'c_0',
Expand Down Expand Up @@ -35,7 +52,7 @@ describe('Tracks Page', () => {
},
];

const { getByText } = await renderApollo(<Tracks />, {
const { getByText } = await renderApolloWithRouter(<Tracks />, {
mocks,
cache,
});
Expand Down
9 changes: 9 additions & 0 deletions final/client/src/utils/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { render } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom'
import '@testing-library/jest-dom/extend-expect';
import { MockedProvider } from '@apollo/client/testing';

Expand All @@ -22,5 +23,13 @@ const renderApollo = (
);
};

export const renderWithRouterGenerator = (renderer) => (node, options) => {
return renderer(<BrowserRouter>{node}</BrowserRouter>, options)
}

export const renderWithRouter = renderWithRouterGenerator(render)

export const renderApolloWithRouter = renderWithRouterGenerator(renderApollo)

export * from '@testing-library/react';
export { renderApollo };

0 comments on commit 9eb263f

Please sign in to comment.