generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Referential stability for the state updater function (#841)
- Loading branch information
Showing
24 changed files
with
237 additions
and
56 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/e2e/next/cypress/e2e/shared/referential-stability.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { testReferentialStability } from 'e2e-shared/specs/referential-stability.cy' | ||
|
||
testReferentialStability({ | ||
path: '/app/referential-stability/useQueryState', | ||
hook: 'useQueryState', | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/app/referential-stability/useQueryStates', | ||
hook: 'useQueryStates', | ||
nextJsRouter: 'app' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/pages/referential-stability/useQueryState', | ||
hook: 'useQueryState', | ||
nextJsRouter: 'pages' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/pages/referential-stability/useQueryStates', | ||
hook: 'useQueryStates', | ||
nextJsRouter: 'pages' | ||
}) |
10 changes: 10 additions & 0 deletions
10
packages/e2e/next/src/app/app/(shared)/referential-stability/useQueryState/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ReferentialStabilityUseQueryState } from 'e2e-shared/specs/referential-stability' | ||
import { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<ReferentialStabilityUseQueryState /> | ||
</Suspense> | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/e2e/next/src/app/app/(shared)/referential-stability/useQueryStates/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ReferentialStabilityUseQueryStates } from 'e2e-shared/specs/referential-stability' | ||
import { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<ReferentialStabilityUseQueryStates /> | ||
</Suspense> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/e2e/next/src/pages/pages/referential-stability/useQueryState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryState } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryState |
3 changes: 3 additions & 0 deletions
3
packages/e2e/next/src/pages/pages/referential-stability/useQueryStates.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryStates } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryStates |
11 changes: 11 additions & 0 deletions
11
packages/e2e/react-router/v6/cypress/e2e/shared/referential-stability.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { testReferentialStability } from 'e2e-shared/specs/referential-stability.cy' | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryState', | ||
hook: 'useQueryState' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryStates', | ||
hook: 'useQueryStates' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/e2e/react-router/v6/src/routes/referential-stability.useQueryState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryState } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryState |
3 changes: 3 additions & 0 deletions
3
packages/e2e/react-router/v6/src/routes/referential-stability.useQueryStates.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryStates } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryStates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/e2e/react-router/v7/app/routes/referential-stability.useQueryState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryState } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryState |
3 changes: 3 additions & 0 deletions
3
packages/e2e/react-router/v7/app/routes/referential-stability.useQueryStates.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryStates } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryStates |
11 changes: 11 additions & 0 deletions
11
packages/e2e/react-router/v7/cypress/e2e/shared/referential-stability.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { testReferentialStability } from 'e2e-shared/specs/referential-stability.cy' | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryState', | ||
hook: 'useQueryState' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryStates', | ||
hook: 'useQueryStates' | ||
}) |
11 changes: 11 additions & 0 deletions
11
packages/e2e/react/cypress/e2e/shared/referential-stability.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { testReferentialStability } from 'e2e-shared/specs/referential-stability.cy' | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryState', | ||
hook: 'useQueryState' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryStates', | ||
hook: 'useQueryStates' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/e2e/react/src/routes/referential-stability.useQueryState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryState } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryState |
3 changes: 3 additions & 0 deletions
3
packages/e2e/react/src/routes/referential-stability.useQueryStates.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryStates } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryStates |
3 changes: 3 additions & 0 deletions
3
packages/e2e/remix/app/routes/referential-stability.useQueryState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryState } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryState |
3 changes: 3 additions & 0 deletions
3
packages/e2e/remix/app/routes/referential-stability.useQueryStates.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReferentialStabilityUseQueryStates } from 'e2e-shared/specs/referential-stability' | ||
|
||
export default ReferentialStabilityUseQueryStates |
11 changes: 11 additions & 0 deletions
11
packages/e2e/remix/cypress/e2e/shared/referential-stability.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { testReferentialStability } from 'e2e-shared/specs/referential-stability.cy' | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryState', | ||
hook: 'useQueryState' | ||
}) | ||
|
||
testReferentialStability({ | ||
path: '/referential-stability/useQueryStates', | ||
hook: 'useQueryStates' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createTest } from '../create-test' | ||
|
||
export const testReferentialStability = createTest( | ||
'Referential stability', | ||
({ path }) => { | ||
it('keeps referential stability of the setter function across updates', () => { | ||
cy.visit(path) | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#state').should('have.text', 'pass') | ||
cy.get('button').click() | ||
cy.get('#state').should('have.text', 'pass') | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client' | ||
|
||
import { parseAsString, useQueryState, useQueryStates } from 'nuqs' | ||
import { useRef } from 'react' | ||
|
||
export function ReferentialStabilityUseQueryState() { | ||
const [, setState] = useQueryState('test') | ||
const setterRef = useRef(setState) | ||
const hasChanged = setterRef.current !== setState | ||
return ( | ||
<> | ||
<button onClick={() => setState('test')}>Test</button> | ||
<div id="state">{hasChanged ? 'fail' : 'pass'}</div> | ||
</> | ||
) | ||
} | ||
|
||
export function ReferentialStabilityUseQueryStates() { | ||
const [, setState] = useQueryStates({ | ||
test: parseAsString | ||
}) | ||
const setterRef = useRef(setState) | ||
const hasChanged = setterRef.current !== setState | ||
return ( | ||
<> | ||
<button onClick={() => setState({ test: 'test' })}>Test</button> | ||
<div id="state">{hasChanged ? 'fail' : 'pass'}</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters