Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/QRCodeCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
if (isSupportPath2d) {
ctx.fill(new Path2D(generatePath(cellsToDraw, margin)));
} else {
cells.forEach((row, rdx) => {
cellsToDraw.forEach((row, rdx) => {
row.forEach((cell, cdx) => {
if (cell) {
ctx.fillRect(cdx + margin, rdx + margin, 1, 1);
Expand Down
46 changes: 46 additions & 0 deletions tests/fallback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { render } from '@testing-library/react';
import React from 'react';

jest.mock('../src/utils', () => ({
...jest.requireActual('../src/utils'),
isSupportPath2d: false,
}));

import { QRCodeCanvas } from '../src/QRCodeCanvas';

describe('Canvas fallback rendering', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('excavates modules behind an embedded image without Path2D', () => {
jest
.spyOn(HTMLImageElement.prototype, 'complete', 'get')
.mockReturnValue(true);
jest
.spyOn(HTMLImageElement.prototype, 'naturalHeight', 'get')
.mockReturnValue(128);
jest
.spyOn(HTMLImageElement.prototype, 'naturalWidth', 'get')
.mockReturnValue(128);
const fillRect = jest.spyOn(CanvasRenderingContext2D.prototype, 'fillRect');

render(
<QRCodeCanvas
value="https://ant.design"
imageSettings={{
src: 'logo.png',
width: 128,
height: 128,
x: 0,
y: 0,
excavate: true,
}}
/>,
);

// The full-size image excavates the complete QR matrix, leaving only the
// background fill. Individual dark modules must not be painted beneath it.
expect(fillRect).toHaveBeenCalledTimes(1);
});
});