-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Tests for canvas fallback heic converter #71052
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
Merged
inimaga
merged 2 commits into
Expensify:main
from
callstack-internal:tests-for-canvas-fallback-heic-converter
Sep 24, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -81,4 +81,102 @@ describe('FileUtils', () => { | |
| expect(error).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('canvasFallback', () => { | ||
| const mockCreateImageBitmap = jest.fn(); | ||
| const mockCanvas = { | ||
| width: 0, | ||
| height: 0, | ||
| getContext: jest.fn(), | ||
| toBlob: jest.fn(), | ||
| }; | ||
| const mockCtx = { | ||
| drawImage: jest.fn(), | ||
| }; | ||
| const mockCreateElement = jest.fn(); | ||
| const mockURL = { | ||
| createObjectURL: jest.fn(() => 'blob:mock-url'), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| (global as any).createImageBitmap = mockCreateImageBitmap; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| (global as any).document = { | ||
| createElement: mockCreateElement, | ||
| }; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| (global as any).URL = mockURL; | ||
|
|
||
| mockCreateElement.mockReturnValue(mockCanvas); | ||
| mockCanvas.getContext.mockReturnValue(mockCtx); | ||
| mockCreateImageBitmap.mockResolvedValue({ | ||
| width: 1000, | ||
| height: 800, | ||
| close: jest.fn(), | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| delete (global as any).createImageBitmap; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| delete (global as any).document; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| delete (global as any).URL; | ||
| }); | ||
|
|
||
| it('should reject when createImageBitmap is undefined', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| delete (global as any).createImageBitmap; | ||
|
|
||
| const blob = new Blob(['test'], {type: 'image/heic'}); | ||
|
|
||
| await expect(FileUtils.canvasFallback(blob, 'test.heic')).rejects.toThrow('Canvas fallback not supported in this browser'); | ||
| }); | ||
|
|
||
| it('should successfully convert HEIC to JPEG', async () => { | ||
| const blob = new Blob(['test'], {type: 'image/heic'}); | ||
| const mockBlob = new Blob(['converted'], {type: 'image/jpeg'}); | ||
| mockCanvas.toBlob.mockImplementation((callback: (blob: Blob | null) => void) => callback(mockBlob)); | ||
|
|
||
| const result = await FileUtils.canvasFallback(blob, 'expense.heic'); | ||
|
|
||
| expect(result).toBeInstanceOf(File); | ||
| expect(result.type).toBe(CONST.IMAGE_FILE_FORMAT.JPEG); | ||
| expect(result.name).toBe('expense.jpg'); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
| expect((result as any).uri).toBe('blob:mock-url'); | ||
|
Comment on lines
+150
to
+151
|
||
| }); | ||
|
|
||
| it('should scale down large images', async () => { | ||
| const blob = new Blob(['test'], {type: 'image/heic'}); | ||
| const mockImageBitmap = {width: 8192, height: 4000, close: jest.fn()}; | ||
| mockCreateImageBitmap.mockResolvedValue(mockImageBitmap); | ||
|
|
||
| const mockBlob = new Blob(['converted'], {type: 'image/jpeg'}); | ||
| mockCanvas.toBlob.mockImplementation((callback: (blob: Blob | null) => void) => callback(mockBlob)); | ||
|
|
||
| await FileUtils.canvasFallback(blob, 'test.heic'); | ||
|
|
||
| expect(mockCanvas.width).toBe(4096); | ||
| expect(mockCanvas.height).toBe(2000); | ||
| }); | ||
|
|
||
| it('should reject when canvas context is null', async () => { | ||
| const blob = new Blob(['test'], {type: 'image/heic'}); | ||
| mockCanvas.getContext.mockReturnValue(null); | ||
|
|
||
| await expect(FileUtils.canvasFallback(blob, 'test.heic')).rejects.toThrow('Could not get canvas context'); | ||
| }); | ||
|
|
||
| it('should reject when toBlob returns null', async () => { | ||
| const blob = new Blob(['test'], {type: 'image/heic'}); | ||
| mockCanvas.toBlob.mockImplementation((callback: (blob: Blob | null) => void) => callback(null)); | ||
|
|
||
| await expect(FileUtils.canvasFallback(blob, 'test.heic')).rejects.toThrow('Canvas conversion failed - returned null blob'); | ||
| }); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The repeated eslint-disable comments create visual clutter. Consider extracting the global object assignments to helper functions or using a more concise approach to reduce the duplication of these long disable comments.