Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: suite for expected global object behaviour #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/__tests__/global-object/__fixtures__/pages/gip.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ 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', () => {
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