Skip to content

Commit

Permalink
test: suite for expected global object behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Meemaw committed Dec 17, 2020
1 parent 7103bfb commit 01ba581
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
26 changes: 22 additions & 4 deletions src/__tests__/global-object/__fixtures__/pages/gip.js
@@ -1,28 +1,46 @@
import React from 'react';
import { PropsPrinter } from '../../../__utils__';

const window_moduleLoadTime = typeof window !== 'undefined';
const document_moduleLoadTime = typeof document !== 'undefined';
const isWeb_moduleLoadTime = window_moduleLoadTime && document_moduleLoadTime;

export default function gip(props) {
const window_componentScope = typeof window !== 'undefined';
const document_componentScope = typeof document !== 'undefined';
const isWeb_componentScope = window_componentScope && document_componentScope;

return (
<>
<h2>Page</h2>
<PropsPrinter
props={{
...props,
window_componentScope: typeof window !== 'undefined',
document_componentScope: typeof document !== 'undefined',
/* Should be hydrated "client" side where browser is defined */
window_componentScope,
document_componentScope,
isWeb_componentScope,
window_componentScope_moduleLoadTime: window_moduleLoadTime,
document_componentScope_moduleLoadTime: document_moduleLoadTime,
isWeb_componentScope_moduleLoadTime: isWeb_moduleLoadTime,
}}
/>
</>
);
}

gip.getInitialProps = async () => {
const window_dataFetchingScope = typeof window !== 'undefined';
const document_dataFetchingScope = typeof document !== 'undefined';
const isWeb_dataFetchingScope =
window_dataFetchingScope && document_dataFetchingScope;

return {
window_moduleLoadTime,
document_moduleLoadTime,
window_dataFetchingScope: typeof window !== 'undefined',
document_dataFetchingScope: typeof document !== 'undefined',
isWeb_moduleLoadTime,
window_dataFetchingScope,
document_dataFetchingScope,
isWeb_dataFetchingScope,
};
};
26 changes: 22 additions & 4 deletions src/__tests__/global-object/__fixtures__/pages/ssr.js
@@ -1,30 +1,48 @@
import React from 'react';
import { PropsPrinter } from '../../../__utils__';

const window_moduleLoadTime = typeof window !== 'undefined';
const document_moduleLoadTime = typeof document !== 'undefined';
const isWeb_moduleLoadTime = window_moduleLoadTime && document_moduleLoadTime;

export default function ssr(props) {
const window_componentScope = typeof window !== 'undefined';
const document_componentScope = typeof document !== 'undefined';
const isWeb_componentScope = window_componentScope && document_componentScope;

return (
<>
<h2>Page</h2>
<PropsPrinter
props={{
...props,
window_componentScope: typeof window !== 'undefined',
document_componentScope: typeof document !== 'undefined',
/* Should be hydrated "client" side where browser is defined */
window_componentScope,
document_componentScope,
isWeb_componentScope,
window_componentScope_moduleLoadTime: window_moduleLoadTime,
document_componentScope_moduleLoadTime: document_moduleLoadTime,
isWeb_componentScope_moduleLoadTime: isWeb_moduleLoadTime,
}}
/>
</>
);
}

export async function getServerSideProps() {
const window_dataFetchingScope = typeof window !== 'undefined';
const document_dataFetchingScope = typeof document !== 'undefined';
const isWeb_dataFetchingScope =
window_dataFetchingScope && document_dataFetchingScope;

return {
props: {
isWeb_moduleLoadTime,
window_moduleLoadTime,
document_moduleLoadTime,
window_dataFetchingScope: typeof window !== 'undefined',
document_dataFetchingScope: typeof document !== 'undefined',
window_dataFetchingScope,
document_dataFetchingScope,
isWeb_dataFetchingScope,
},
};
}
Expand Up @@ -7,22 +7,33 @@ import GIPPage from './__fixtures__/pages/gip';
import path from 'path';

const expectedGlobals = {
window_moduleLoadTime: true,
document_moduleLoadTime: true,
window_moduleLoadTime: false,
document_moduleLoadTime: false,
isWeb_moduleLoadTime: false,

window_dataFetchingScope: false,
document_dataFetchingScope: false,
isWeb_dataFetchingScope: false,

window_componentScope: true,
document_componentScope: true,
isWeb_componentScope: true,

window_componentScope_moduleLoadTime: true,
document_componentScope_moduleLoadTime: true,
isWeb_componentScope_moduleLoadTime: true,
};

const expectedGlobals_GIPClientSide = {
...expectedGlobals,

window_dataFetchingScope: true,
document_dataFetchingScope: true,
isWeb_dataFetchingScope: true,
};

describe('Global object', () => {
describe('getServerSideProps', () => {
describe.only('getServerSideProps', () => {
describe.each(['initial', 'client'])('%s render', (renderType) => {
it("executes page's exports with expected env globals", async () => {
const initialRoute = renderType === 'client' ? '/' : '/ssr';
Expand All @@ -32,6 +43,7 @@ describe('Global object', () => {
});
const { container: actual } = render(page);

// Client side navigation to SSR page
if (renderType === 'client') {
userEvent.click(screen.getByText('Go to SSR'));
await screen.findByText('Page');
Expand Down

0 comments on commit 01ba581

Please sign in to comment.