Repository navigation
fix(h5): validate canvas export targets #19564
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { canvasToTempFilePath } from '../../src/api/canvas/canvasToTempFilePath' | ||
| import * as utils from '../../src/utils' | ||
|
|
||
| describe('canvas export', () => { | ||
| let container: HTMLDivElement | ||
| let canvas: HTMLCanvasElement | ||
| let otherCanvas: HTMLCanvasElement | ||
|
|
||
| beforeEach(() => { | ||
| container = document.createElement('div') | ||
| otherCanvas = document.createElement('canvas') | ||
| otherCanvas.setAttribute('canvas-id', 'other') | ||
| container.appendChild(otherCanvas) | ||
| canvas = document.createElement('canvas') | ||
| canvas.setAttribute('canvas-id', 'test') | ||
| container.appendChild(canvas) | ||
| jest.spyOn(utils, 'findDOM').mockReturnValue(container) | ||
| }) | ||
|
|
||
| afterEach(() => jest.restoreAllMocks()) | ||
|
|
||
| test.each(['missing canvas', 'missing container'])('rejects a %s', async scenario => { | ||
| if (scenario === 'missing container') jest.mocked(utils.findDOM).mockReturnValue(undefined) | ||
| await expect(canvasToTempFilePath({ canvasId: 'missing' })).rejects.toEqual({ | ||
| errMsg: 'canvasToTempFilePath:fail canvas missing is not available' | ||
| }) | ||
| }) | ||
|
|
||
| test('calls fail and complete once without reporting success for a missing canvas', async () => { | ||
| const success = jest.fn() | ||
| const fail = jest.fn() | ||
| const complete = jest.fn() | ||
| const expected = { errMsg: 'canvasToTempFilePath:fail canvas missing is not available' } | ||
| await expect(canvasToTempFilePath({ canvasId: 'missing', success, fail, complete })).resolves.toEqual(expected) | ||
| expect(success).not.toHaveBeenCalled() | ||
| expect(fail).toHaveBeenCalledTimes(1) | ||
| expect(fail).toHaveBeenCalledWith(expected) | ||
| expect(complete).toHaveBeenCalledTimes(1) | ||
| expect(complete).toHaveBeenCalledWith(expected) | ||
| }) | ||
|
|
||
| test('routes DOM lookup failures through the failure handler', async () => { | ||
| jest.mocked(utils.findDOM).mockImplementation(() => { throw new Error('Canvas lookup failed') }) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里目前只验证了 Promise reject,没有覆盖 callback 模式。建议传入
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已在 790b975 中保留原有 Promise reject 测试,并补充 DOM 查找抛错时的回调用例:同时传入 |
||
| await expect(canvasToTempFilePath({ canvasId: 'test' })).rejects.toEqual({ | ||
| errMsg: 'canvasToTempFilePath:fail Canvas lookup failed' | ||
| }) | ||
| }) | ||
|
|
||
| test('routes invalid selector errors through the failure handler', async () => { | ||
| await expect(canvasToTempFilePath({ canvasId: '"]' })).rejects.toEqual({ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议补充 callback 断言:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已在 790b975 中保留非法选择器的 Promise reject 测试,并新增回调模式覆盖:断言 |
||
| errMsg: expect.stringMatching(/^canvasToTempFilePath:fail /) | ||
| }) | ||
| }) | ||
|
|
||
| test.each(['DOM lookup failure', 'invalid selector'])('calls failure callbacks once for a %s', async scenario => { | ||
| const success = jest.fn() | ||
| const fail = jest.fn() | ||
| const complete = jest.fn() | ||
| const canvasId = scenario === 'invalid selector' ? '"]' : 'test' | ||
| if (scenario === 'DOM lookup failure') { | ||
| jest.mocked(utils.findDOM).mockImplementation(() => { throw new Error('Canvas lookup failed') }) | ||
| } | ||
| const expected = { | ||
| errMsg: scenario === 'DOM lookup failure' | ||
| ? 'canvasToTempFilePath:fail Canvas lookup failed' | ||
| : expect.stringMatching(/^canvasToTempFilePath:fail /) | ||
| } | ||
|
|
||
| await expect(canvasToTempFilePath({ canvasId, success, fail, complete })).resolves.toEqual(expected) | ||
| expect(success).not.toHaveBeenCalled() | ||
| expect(fail).toHaveBeenCalledTimes(1) | ||
| expect(fail).toHaveBeenCalledWith(expected) | ||
| expect(complete).toHaveBeenCalledTimes(1) | ||
| expect(complete).toHaveBeenCalledWith(expected) | ||
| }) | ||
|
|
||
| test('reports security errors from exporting a tainted canvas', async () => { | ||
| jest.spyOn(canvas, 'toDataURL').mockImplementation(() => { | ||
| throw new DOMException('Canvas is tainted', 'SecurityError') | ||
| }) | ||
| const fail = jest.fn() | ||
| const complete = jest.fn() | ||
| const expected = { errMsg: 'canvasToTempFilePath:fail Canvas is tainted' } | ||
| await expect(canvasToTempFilePath({ canvasId: 'test', fail, complete })).resolves.toEqual(expected) | ||
| expect(fail).toHaveBeenCalledTimes(1) | ||
| expect(fail).toHaveBeenCalledWith(expected) | ||
| expect(complete).toHaveBeenCalledTimes(1) | ||
| expect(complete).toHaveBeenCalledWith(expected) | ||
| }) | ||
|
|
||
| test.each([ | ||
| [undefined, 'image/png'], | ||
| ['png', 'image/png'], | ||
| ['jpg', 'image/jpeg'] | ||
| ] as const)('exports %s with the requested quality', async (fileType, mimeType) => { | ||
| const tempFilePath = `data:${mimeType};base64,test` | ||
| const toDataURL = jest.spyOn(canvas, 'toDataURL').mockReturnValue(tempFilePath) | ||
| const otherToDataURL = jest.spyOn(otherCanvas, 'toDataURL').mockReturnValue(`data:${mimeType};base64,other`) | ||
| const success = jest.fn() | ||
| const fail = jest.fn() | ||
| const complete = jest.fn() | ||
| const expected = { errMsg: 'canvasToTempFilePath:ok', tempFilePath } | ||
| await expect(canvasToTempFilePath({ canvasId: 'test', fileType, quality: 0.8, success, fail, complete })).resolves.toEqual(expected) | ||
| expect(toDataURL).toHaveBeenCalledTimes(1) | ||
| expect(toDataURL).toHaveBeenCalledWith(mimeType, 0.8) | ||
| expect(otherToDataURL).not.toHaveBeenCalled() | ||
| expect(success).toHaveBeenCalledTimes(1) | ||
| expect(success).toHaveBeenCalledWith(expected) | ||
| expect(fail).not.toHaveBeenCalled() | ||
| expect(complete).toHaveBeenCalledTimes(1) | ||
| expect(complete).toHaveBeenCalledWith(expected) | ||
| }) | ||
| }) | ||
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.
建议在这里再添加一个不同
canvas-id的 canvas,并分别 spy 两个 canvas 的toDataURL。当前测试容器里只有一个 canvas,即使实现错误地始终选择第一个 canvas,测试也会通过,无法验证canvasId是否真正生效。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.
感谢建议,已在 790b975 中添加另一个
canvas-id="other"的画布,并将其放在目标画布之前。成功导出测试分别 spy 两个画布的toDataURL,断言目标画布调用一次、非目标画布不调用,且返回目标画布的数据,覆盖误选第一个画布的情况。Canvas 相关 2 个测试套件、15 个用例全部通过,修改文件 ESLint 和差异检查通过,烦请复查。