Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
fix(apollo): test coverage put to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Danikowski authored and Kevin Danikowski committed Jan 22, 2020
1 parent e28bcd7 commit 7f40b76
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 35 deletions.
1 change: 0 additions & 1 deletion templates/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"<rootDir>/.next/",
"<rootDir>/node_modules/",
"<rootDir>/coverage/",
"<rootDir>/src/utils/apollo.jsx",
"/__mocks__/"
],
"coverageReporters": ["clover", "json", "json-summary", "lcov", "text"],
Expand Down
38 changes: 37 additions & 1 deletion templates/default/src/utils/__snapshots__/apollo.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`apollo should run without error 1`] = `
exports[`apollo client side with getInitialProps 1`] = `Promise {}`;

exports[`apollo client side with getInitialProps 2`] = `
<DocumentFragment>
<div
data-testid="sample-app"
>
OK!
</div>
</DocumentFragment>
`;

exports[`apollo server side 1`] = `
<DocumentFragment>
<div
data-testid="sample-app"
>
OK!
</div>
</DocumentFragment>
`;

exports[`apollo server side with getInitialProps finished 1`] = `Promise {}`;

exports[`apollo server side with getInitialProps finished 2`] = `
<DocumentFragment>
<div
data-testid="sample-app"
>
OK!
</div>
</DocumentFragment>
`;

exports[`apollo server side with getInitialProps unfinished 1`] = `Promise {}`;

exports[`apollo server side with getInitialProps unfinished 2`] = `
<DocumentFragment>
<div
data-testid="sample-app"
Expand Down
24 changes: 5 additions & 19 deletions templates/default/src/utils/apollo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let apolloClient = null
*/
function createApolloClient(initialState = {}) {
return new ApolloClient({
ssrMode: false,
ssrMode: false, // !process.browser
link: new HttpLink({
uri: apiGatewayUrl, // Server URL (must be absolute)
// credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
Expand All @@ -39,7 +39,8 @@ function createApolloClient(initialState = {}) {
function initApolloClient(initialState) {
// Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad)
if (typeof window === 'undefined') {

if (!process.browser) {
return createApolloClient(initialState)
}

Expand Down Expand Up @@ -69,18 +70,6 @@ export function withApollo(PageComponent) {
)
}

// Set the correct displayName in development
if (process.env.NODE_ENV !== 'production') {
const displayName =
PageComponent.displayName || PageComponent.name || 'Component'

if (displayName === 'App') {
console.warn('This withApollo HOC only works with PageComponents.')
}

WithApollo.displayName = `withApollo(${displayName})`
}

if (PageComponent.getInitialProps) {
WithApollo.getInitialProps = async ctx => {
// Initialize ApolloClient, add it to the ctx object so
Expand All @@ -89,13 +78,10 @@ export function withApollo(PageComponent) {
const apolloClient = (ctx.apolloClient = initApolloClient())

// Run wrapped getInitialProps methods
let pageProps = {}
if (PageComponent.getInitialProps) {
pageProps = await PageComponent.getInitialProps(ctx)
}
const pageProps = await PageComponent.getInitialProps(ctx)

// Only on the server:
if (typeof window === 'undefined') {
if (!process.browser) {
// When redirecting, the response is finished.
// No point in continuing to render
if (ctx.res && ctx.res.finished) {
Expand Down
64 changes: 50 additions & 14 deletions templates/default/src/utils/apollo.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,63 @@ jest.mock('next/config', () => () => ({
}
}))

describe('apollo', () => {
// eslint-disable-next-line
class SampleApp extends Component {
render() {
return <div data-testid="sample-app">OK!</div>
}
// eslint-disable-next-line
class AppWithProps extends Component {
render() {
return <div data-testid="sample-app">OK!</div>
}
}

SampleApp.getInitialProps = async appContext => {
const appProps = await SampleApp.getInitialProps(appContext)
// eslint-disable-next-line
const AppWithNoProps = () => <div data-testid="sample-app">OK!</div>

return { ...appProps }
}
describe('apollo', () => {
it('server side', () => {
const SampleComponentWithApollo = withApollo(AppWithNoProps)
const { asFragment, getByTestId } = render(<SampleComponentWithApollo />)

it('should run without error', () => {
const SampleComponentWithApollo = withApollo(SampleApp)
// call below for greater coverage, but currently ctx is undefined
// SampleComponentWithApollo.getInitialProps()
expect(getByTestId('sample-app')).toBeTruthy()
expect(asFragment()).toMatchSnapshot()
})

it('server side with getInitialProps unfinished', () => {
const mockCtx = {}
AppWithProps.getInitialProps = jest.fn().mockResolvedValue(mockCtx)

const SampleComponentWithApollo = withApollo(AppWithProps)
const { asFragment, getByTestId } = render(<SampleComponentWithApollo />)

expect(SampleComponentWithApollo.getInitialProps(mockCtx)).toMatchSnapshot()
expect(getByTestId('sample-app')).toBeTruthy()
expect(asFragment()).toMatchSnapshot()
})

it('server side with getInitialProps finished', () => {
const mockCtx = { res: { finished: true } }
AppWithProps.getInitialProps = jest.fn().mockResolvedValue(mockCtx)

const SampleComponentWithApollo = withApollo(AppWithProps)
const { asFragment, getByTestId } = render(<SampleComponentWithApollo />)

expect(SampleComponentWithApollo.getInitialProps(mockCtx)).toMatchSnapshot()
expect(getByTestId('sample-app')).toBeTruthy()
expect(asFragment()).toMatchSnapshot()
})

it('client side with getInitialProps', () => {
jest.resetModules() // this is important - it clears the cache
process.browser = true

const mockCtx = {}
AppWithProps.getInitialProps = jest.fn().mockResolvedValue(mockCtx)

const SampleComponentWithApollo = withApollo(AppWithProps)
const { asFragment, getByTestId } = render(<SampleComponentWithApollo />)

expect(SampleComponentWithApollo.getInitialProps(mockCtx)).toMatchSnapshot()
expect(getByTestId('sample-app')).toBeTruthy()
expect(asFragment()).toMatchSnapshot()

jest.resetModules()
})
})

0 comments on commit 7f40b76

Please sign in to comment.