Skip to content

Commit 3c0ad34

Browse files
authored
graphiql 5: run cypress tests in React strict mode (#4020)
* run cypress tests in React strict mode * upd * upd * upd * Update packages/graphiql/cypress/e2e/tabs.cy.ts
1 parent d4950df commit 3c0ad34

File tree

9 files changed

+42
-27
lines changed

9 files changed

+42
-27
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphiql/react': patch
3+
---
4+
5+
- run cypress tests in React strict mode
6+
- fix `defaultQuery` with empty string does not result in an empty default query
7+
- fix `useDidUpdate` in React strict mode

packages/graphiql-react/src/components/provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const InnerGraphiQLProvider: FC<InnerGraphiQLProviderProps> = ({
145145

146146
const { tabs, activeTabIndex } = getDefaultTabState({
147147
defaultHeaders,
148-
defaultQuery: defaultQuery || DEFAULT_QUERY,
148+
defaultQuery: defaultQuery ?? DEFAULT_QUERY,
149149
defaultTabs,
150150
headers,
151151
query,

packages/graphiql-react/src/utility/hooks.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,21 @@ export function useOptimisticState([
173173
return [state, setState];
174174
}
175175

176-
export const useDidUpdate: typeof useEffect = (callback, dependencies) => {
176+
// https://github.com/mantinedev/mantine/blob/master/packages/@mantine/hooks/src/use-did-update/use-did-update.ts
177+
export const useDidUpdate: typeof useEffect = (fn, dependencies) => {
177178
const didMountRef = useRef(false);
178179

180+
// React Strict Mode intentionally mounts → unmounts → mounts the component during development.
179181
useEffect(() => {
180-
if (!didMountRef.current) {
181-
didMountRef.current = true;
182-
return;
182+
return () => {
183+
didMountRef.current = false;
184+
};
185+
}, []);
186+
187+
useEffect(() => {
188+
if (didMountRef.current) {
189+
return fn();
183190
}
184-
callback();
191+
didMountRef.current = true;
185192
}, dependencies); // eslint-disable-line react-hooks/exhaustive-deps
186193
};

packages/graphiql/cypress/e2e/headers.cy.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ const DEFAULT_HEADERS = '{"foo":2}';
33
describe('Headers', () => {
44
describe('`defaultHeaders`', () => {
55
it('should have default headers while open new tabs', () => {
6-
cy.visit(
7-
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&defaultQuery=`,
8-
);
6+
cy.visit(`?query={test}&defaultHeaders=${DEFAULT_HEADERS}`);
97
cy.assertHasValues({ query: '{test}', headersString: DEFAULT_HEADERS });
108
cy.get('.graphiql-tab-add').click();
119
cy.assertHasValues({ query: '', headersString: DEFAULT_HEADERS });
@@ -16,7 +14,7 @@ describe('Headers', () => {
1614
it('in case `headers` and `defaultHeaders` are set, `headers` should be on 1st tab and `defaultHeaders` for other opened tabs', () => {
1715
const HEADERS = '{"bar":true}';
1816
cy.visit(
19-
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&headers=${HEADERS}&defaultQuery=`,
17+
`?query={test}&defaultHeaders=${DEFAULT_HEADERS}&headers=${HEADERS}`,
2018
);
2119
cy.assertHasValues({ query: '{test}', headersString: HEADERS });
2220
cy.get('.graphiql-tab-add').click();

packages/graphiql/cypress/e2e/history.cy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ describe('history', () => {
1515
});
1616

1717
it('will save history item even when history panel is closed', () => {
18-
cy.visit('/?query={test}');
18+
cy.visit('?query={test}');
1919
cy.clickExecuteQuery();
2020
cy.get('button[aria-label="Show History"]').click();
2121
cy.get('ul.graphiql-history-items').should('have.length', 1);
2222
});
2323

2424
it('will save history item even when history panel is closed', () => {
25-
cy.visit('/?query={test}');
25+
cy.visit('?query={test}');
2626
cy.clickExecuteQuery();
2727
cy.get('button[aria-label="Show History"]').click();
2828
cy.get('ul.graphiql-history-items li').should('have.length', 1);

packages/graphiql/cypress/e2e/prettify.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describeOrSkip('GraphiQL Prettify', () => {
2929
it('should work while click on prettify button', () => {
3030
const rawQuery = '{ test\n\nid }';
3131
const resultQuery = '{ test id }';
32-
cy.visit(`/?query=${rawQuery}&onPrettifyQuery=true`);
32+
cy.visit(`?query=${rawQuery}&onPrettifyQuery=true`);
3333
cy.clickPrettify();
3434
cy.assertHasValues({ query: resultQuery });
3535
});

packages/graphiql/cypress/e2e/tabs.cy.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
describe('Tabs', () => {
22
it('Should store editor contents when switching between tabs', () => {
3-
cy.visit('/?defaultQuery=&query=');
3+
cy.visit('?defaultQuery=');
44

55
// Assert that tab visible when there's only one session
66
cy.get('.graphiql-tab-button').eq(0).should('exist');
7-
87
// Enter a query without operation name
98
cy.get('.graphiql-query-editor textarea').type('{id', { force: true });
109

1110
// Run the query
1211
cy.clickExecuteQuery();
12+
// Assert request is not cancelled
13+
cy.get('.result-window').should('not.have.text', '');
1314

1415
// Open a new tab
1516
cy.get('.graphiql-tab-add').click();
@@ -33,7 +34,8 @@ describe('Tabs', () => {
3334

3435
// Run the query
3536
cy.clickExecuteQuery();
36-
37+
// Assert request is not cancelled
38+
cy.get('.result-window').should('not.have.text', '');
3739
// Switch back to the first tab
3840
cy.get('.graphiql-tab-button').eq(0).click();
3941

@@ -82,7 +84,7 @@ describe('Tabs', () => {
8284
describe('confirmCloseTab()', () => {
8385
it('should keep tab when `Cancel` was clicked', () => {
8486
cy.on('window:confirm', () => false);
85-
cy.visit('/?confirmCloseTab=true');
87+
cy.visit('?confirmCloseTab=true');
8688

8789
cy.get('.graphiql-tab-add').click();
8890

@@ -93,7 +95,7 @@ describe('Tabs', () => {
9395

9496
it('should close tab when `OK` was clicked', () => {
9597
cy.on('window:confirm', () => true);
96-
cy.visit('/?confirmCloseTab=true');
98+
cy.visit('?confirmCloseTab=true');
9799

98100
cy.get('.graphiql-tab-add').click();
99101

packages/graphiql/cypress/e2e/theme.cy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
describe('Theme', () => {
22
describe('`forcedTheme`', () => {
33
it('Switches to light theme when `forcedTheme` is light', () => {
4-
cy.visit('/?forcedTheme=light');
4+
cy.visit('?forcedTheme=light');
55
cy.get('body').should('have.class', 'graphiql-light');
66
});
77

88
it('Switches to dark theme when `forcedTheme` is dark', () => {
9-
cy.visit('/?forcedTheme=dark');
9+
cy.visit('?forcedTheme=dark');
1010
cy.get('body').should('have.class', 'graphiql-dark');
1111
});
1212

1313
it('Defaults to light theme when `forcedTheme` value is invalid', () => {
14-
cy.visit('/?forcedTheme=invalid');
14+
cy.visit('?forcedTheme=invalid');
1515
cy.get('[data-value=settings]').click();
1616
cy.get('.graphiql-dialog-section-title')
1717
.eq(1)
@@ -21,11 +21,11 @@ describe('Theme', () => {
2121

2222
describe('`defaultTheme`', () => {
2323
it('should have light theme', () => {
24-
cy.visit('/?defaultTheme=light');
24+
cy.visit('?defaultTheme=light');
2525
cy.get('body').should('have.class', 'graphiql-light');
2626
});
2727
it('should have dark theme', () => {
28-
cy.visit('/?defaultTheme=dark');
28+
cy.visit('?defaultTheme=dark');
2929
cy.get('body').should('have.class', 'graphiql-dark');
3030
});
3131
});

packages/graphiql/src/e2e.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ const props: ComponentProps<typeof GraphiQL> = {
123123
};
124124

125125
root.render(
126-
// TODO: enable strict mode after monaco-editor migration
127-
// <StrictMode>
128-
React.createElement(GraphiQL, props),
129-
// </StrictMode>,
126+
React.createElement(
127+
React.StrictMode,
128+
null,
129+
React.createElement(GraphiQL, props),
130+
),
130131
);

0 commit comments

Comments
 (0)