-
-
Notifications
You must be signed in to change notification settings - Fork 625
fix(person-images): prevent flash of previous page's images (#1315) #1321
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
Open
tanmaysachann
wants to merge
4
commits into
AOSSIE-Org:main
Choose a base branch
from
tanmaysachann:fix/1315-ai-tagging-page-flash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+186
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c85b57f
fix(person-images): prevent flash of previous page's images (#1315)
tanmaysachann 1eebcba
fix(person-images): guard undefined clusterId and drop the any cast
tanmaysachann 88187a7
refactor: clean up verbose comments
tanmaysachann 0fe7282
fix(person-images): prevent stale heading flash
tanmaysachann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,166 @@ | ||
| import { useLayoutEffect } from 'react'; | ||
| import { fireEvent, render, screen, waitFor } from '@/test-utils'; | ||
| import { Routes, Route, useNavigate } from 'react-router'; | ||
| import { PersonImages } from '@/pages/PersonImages/PersonImages'; | ||
| import type { Image } from '@/types/Media'; | ||
| import * as apiFunctions from '@/api/api-functions'; | ||
|
|
||
| jest.mock('@tauri-apps/api/core', () => ({ | ||
| invoke: jest.fn().mockResolvedValue(null), | ||
| convertFileSrc: (path: string) => path, | ||
| })); | ||
|
|
||
| jest.mock('@/api/api-functions', () => ({ | ||
| fetchClusterImages: jest.fn(), | ||
| renameCluster: jest.fn(), | ||
| })); | ||
|
|
||
| const fetchClusterImages = apiFunctions.fetchClusterImages as jest.Mock; | ||
|
|
||
| const makeImage = (id: string, path: string): Image => ({ | ||
| id, | ||
| path, | ||
| thumbnailPath: path, | ||
| folder_id: 'folder', | ||
| isTagged: true, | ||
| isFavourite: false, | ||
| tags: [], | ||
| }); | ||
|
|
||
| const personAImages = [ | ||
| makeImage('a1', '/personA/1.jpg'), | ||
| makeImage('a2', '/personA/2.jpg'), | ||
| ]; | ||
| const personBImages = [ | ||
| makeImage('b1', '/personB/1.jpg'), | ||
| makeImage('b2', '/personB/2.jpg'), | ||
| ]; | ||
|
|
||
| const CommitProbe = ({ | ||
| onCommit, | ||
| }: { | ||
| onCommit?: (headingText: string | null) => void; | ||
| }) => { | ||
| useLayoutEffect(() => { | ||
| onCommit?.(document.querySelector('h1')?.textContent ?? null); | ||
| }); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| const PersonImagesWithNavigation = ({ | ||
| onCommit, | ||
| }: { | ||
| onCommit?: (headingText: string | null) => void; | ||
| }) => { | ||
| const navigate = useNavigate(); | ||
|
|
||
| return ( | ||
| <> | ||
| <button type="button" onClick={() => navigate('/person/B')}> | ||
| Go to Person B | ||
| </button> | ||
| <PersonImages /> | ||
| <CommitProbe onCommit={onCommit} /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| const renderPerson = ( | ||
| clusterId: string, | ||
| options: { | ||
| withNavigation?: boolean; | ||
| onCommit?: (headingText: string | null) => void; | ||
| } = {}, | ||
| ) => | ||
| render( | ||
| <Routes> | ||
| <Route | ||
| path="/person/:clusterId" | ||
| element={ | ||
| options.withNavigation ? ( | ||
| <PersonImagesWithNavigation onCommit={options.onCommit} /> | ||
| ) : ( | ||
| <PersonImages /> | ||
| ) | ||
| } | ||
| /> | ||
| </Routes>, | ||
| { | ||
| preloadedState: { | ||
| images: { images: personAImages, currentViewIndex: -1 }, | ||
| }, | ||
| initialRoutes: [`/person/${clusterId}`], | ||
| }, | ||
| ); | ||
|
|
||
| const imageSrcs = (container: HTMLElement) => | ||
| Array.from(container.querySelectorAll('img')).map((img) => | ||
| img.getAttribute('src'), | ||
| ); | ||
|
|
||
| const hasPersonA = (container: HTMLElement) => | ||
| imageSrcs(container).some((src) => src?.startsWith('/personA/')); | ||
| const hasPersonB = (container: HTMLElement) => | ||
| imageSrcs(container).some((src) => src?.startsWith('/personB/')); | ||
|
|
||
| beforeEach(() => { | ||
| fetchClusterImages.mockReset(); | ||
| fetchClusterImages.mockImplementation(async ({ clusterId }) => ({ | ||
| success: true, | ||
| data: { | ||
| images: clusterId === 'B' ? personBImages : personAImages, | ||
| cluster_name: clusterId === 'B' ? 'Bob' : 'Alice', | ||
| }, | ||
| })); | ||
| }); | ||
|
|
||
| describe('PersonImages (issue #1315: no flash of the previous page)', () => { | ||
| test('never paints stale images or heading when navigating to a cached person', async () => { | ||
| const committedHeadings: Array<string | null> = []; | ||
| const { container, queryClient } = renderPerson('A', { | ||
| withNavigation: true, | ||
| onCommit: (headingText) => committedHeadings.push(headingText), | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect( | ||
| screen.getByRole('heading', { name: 'Alice' }), | ||
| ).toBeInTheDocument(); | ||
| expect(hasPersonA(container)).toBe(true); | ||
| }); | ||
|
|
||
| queryClient.setQueryData(['person-images', 'B'], { | ||
| success: true, | ||
| data: { | ||
| images: personBImages, | ||
| cluster_name: 'Bob', | ||
| }, | ||
| }); | ||
|
|
||
| committedHeadings.length = 0; | ||
| fireEvent.click(screen.getByRole('button', { name: 'Go to Person B' })); | ||
|
|
||
| expect(committedHeadings).not.toContain('Alice'); | ||
| expect( | ||
| screen.queryByRole('heading', { name: 'Alice' }), | ||
| ).not.toBeInTheDocument(); | ||
| expect(screen.getByRole('heading', { name: 'Bob' })).toBeInTheDocument(); | ||
| expect(hasPersonA(container)).toBe(false); | ||
| expect(hasPersonB(container)).toBe(true); | ||
| }); | ||
|
|
||
| test("renders the navigated cluster's images and name once loaded", async () => { | ||
| const { container } = renderPerson('B'); | ||
|
|
||
| expect( | ||
| screen.queryByRole('heading', { name: 'Alice' }), | ||
| ).not.toBeInTheDocument(); | ||
| expect(hasPersonA(container)).toBe(false); | ||
|
|
||
| expect(await screen.findByText('Bob')).toBeInTheDocument(); | ||
| await waitFor(() => { | ||
| expect(hasPersonB(container)).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.