-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix/interactive message stale image cache key #9903
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
cevn
wants to merge
4
commits into
mattermost:main
Choose a base branch
from
cevn:fix/interactive-message-stale-image-cache-key
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.
+144
−15
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fd10e72
Fix stale attachment image after in-place post edit on mobile
a7970d7
Add regression test for attachment image stale cache key
979f841
Address review: it('should...') test names and assert gallery item count
25c7076
Remove unused useRef import in mm_blocks_image
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
130 changes: 130 additions & 0 deletions
130
...omponents/post_list/post/body/content/message_attachments/attachment_image/index.test.tsx
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,130 @@ | ||
| // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import ProgressiveImage from '@components/progressive_image'; | ||
| import {Screens, Preferences} from '@constants'; | ||
| import {renderWithIntlAndTheme} from '@test/intl-test-helper'; | ||
| import {openGalleryAtIndex} from '@utils/gallery'; | ||
| import {urlSafeBase64Encode} from '@utils/security'; | ||
|
|
||
| import AttachmentImage from './index'; | ||
|
|
||
| jest.mock('@components/progressive_image', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn(() => null), | ||
| })); | ||
|
|
||
| jest.mock('@context/gallery', () => ({ | ||
| GalleryInit: ({children}: {children: React.ReactNode}) => children, | ||
| })); | ||
|
|
||
| jest.mock('@hooks/device', () => ({ | ||
| useIsTablet: () => false, | ||
| })); | ||
|
|
||
| jest.mock('@hooks/gallery', () => ({ | ||
| useGalleryItem: jest.fn((_galleryIdentifier, _index, onPress) => ({ | ||
| ref: {current: null}, | ||
| onGestureEvent: onPress, | ||
| styles: {}, | ||
| })), | ||
| })); | ||
|
|
||
| jest.mock('@utils/gallery', () => ({ | ||
| openGalleryAtIndex: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@utils/url', () => ({ | ||
| ...jest.requireActual('@utils/url'), | ||
| isValidUrl: (url: string) => url.startsWith('https://'), | ||
| })); | ||
|
|
||
| const IMAGE_URL = 'https://example.com/quotes/image/AAPL_day.png'; | ||
| const IMAGE_URL_2 = 'https://example.com/quotes/image/AAPL_week.png'; | ||
| const IMAGE_METADATA = {width: 700, height: 300, format: 'png', frame_count: 0} as PostImage; | ||
|
|
||
| describe('AttachmentImage', () => { | ||
| const theme = Preferences.THEMES.denim; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| function buildElement(props: Partial<React.ComponentProps<typeof AttachmentImage>> = {}) { | ||
| return ( | ||
| <AttachmentImage | ||
| imageUrl={IMAGE_URL} | ||
| imageMetadata={IMAGE_METADATA} | ||
| layoutWidth={400} | ||
| location={Screens.CHANNEL} | ||
| postId='post-id' | ||
| theme={theme} | ||
| {...props} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function renderImage(props: Partial<React.ComponentProps<typeof AttachmentImage>> = {}) { | ||
| return renderWithIntlAndTheme(buildElement(props)); | ||
| } | ||
|
|
||
| function lastProgressiveImageId() { | ||
| const calls = jest.mocked(ProgressiveImage).mock.calls; | ||
| return calls[calls.length - 1][0].id; | ||
| } | ||
|
|
||
| it('renders ProgressiveImage with the image and a cache id derived from imageUrl', () => { | ||
| renderImage(); | ||
|
|
||
| expect(ProgressiveImage).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| imageUri: IMAGE_URL, | ||
| id: `uid-${urlSafeBase64Encode(IMAGE_URL)}`, | ||
| }), | ||
| undefined, | ||
| ); | ||
| }); | ||
|
|
||
| // Regression test for the stale-image bug: when the same post is edited in place | ||
| // to a new image URL, the cache id must follow imageUrl. Previously it was frozen | ||
| // via useRef, so the new image rendered under the old cache key and stayed stale. | ||
| it('updates the cache id when imageUrl changes on re-render', () => { | ||
| const {rerender} = renderImage({imageUrl: IMAGE_URL}); | ||
| const firstId = lastProgressiveImageId(); | ||
| expect(firstId).toBe(`uid-${urlSafeBase64Encode(IMAGE_URL)}`); | ||
|
|
||
| rerender(buildElement({imageUrl: IMAGE_URL_2})); | ||
| const secondId = lastProgressiveImageId(); | ||
|
|
||
| expect(secondId).toBe(`uid-${urlSafeBase64Encode(IMAGE_URL_2)}`); | ||
| expect(secondId).not.toBe(firstId); | ||
| }); | ||
|
|
||
| it('opens the gallery with a cacheKey matching the current imageUrl', () => { | ||
| renderImage(); | ||
|
|
||
| // Trigger the gallery open via the captured gesture handler. | ||
| const {useGalleryItem} = jest.requireMock('@hooks/gallery'); | ||
| const onGestureEvent = jest.mocked(useGalleryItem).mock.results.at(-1)!.value.onGestureEvent; | ||
| onGestureEvent(); | ||
|
|
||
| expect(openGalleryAtIndex).toHaveBeenCalledWith( | ||
| `post-id-AttachmentImage-${Screens.CHANNEL}`, | ||
| 0, | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| id: `uid-${urlSafeBase64Encode(IMAGE_URL)}`, | ||
| cacheKey: `uid-${urlSafeBase64Encode(IMAGE_URL)}`, | ||
| uri: IMAGE_URL, | ||
| }), | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| it('renders an error frame for an invalid url and skips ProgressiveImage', () => { | ||
| renderImage({imageUrl: 'not-a-url'}); | ||
| expect(ProgressiveImage).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
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
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.