Skip to content

Commit 4b096e2

Browse files
authored
Merge pull request #9031 from harshiltewari2004/fix-flaky-download-tests
Wait for the download click instead of a fixed timeout in saveCanvas …
2 parents ce75065 + dcb206f commit 4b096e2

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎test/unit/image/downloading.js‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ const mockAnchorElement = vi.mockObject({
1010
download: null,
1111
click: () => {}
1212
});
13+
14+
let onDownload = null;
15+
16+
mockAnchorElement.click.mockImplementation(()=>{
17+
if(onDownload) onDownload();
18+
});
19+
20+
const downloadHappened = () =>
21+
new Promise(resolve =>{
22+
onDownload = resolve;
23+
});
24+
1325
const originalCreateElement = document.createElement;
1426
vi.spyOn(document, 'createElement').mockImplementation((...args) => {
1527
if (args[0] !== 'a') {
@@ -61,10 +73,6 @@ expect.extend({
6173
}
6274
});
6375

64-
const wait = async time => {
65-
return new Promise(resolve => setTimeout(resolve, time));
66-
};
67-
6876
suite('Downloading', () => {
6977
beforeAll(async function () {
7078
image(mockP5, mockP5Prototype);
@@ -77,6 +85,10 @@ suite('Downloading', () => {
7785
vi.clearAllMocks();
7886
});
7987

88+
beforeEach(() => {
89+
onDownload = null;
90+
});
91+
8092
suite('downloading animated gifs', function () {
8193
let myGif;
8294
const imagePath = '/test/unit/assets/nyan_cat.gif';
@@ -112,24 +124,30 @@ suite('Downloading', () => {
112124
});
113125

114126
test('should download a png file', async () => {
127+
const downloaded = downloadHappened();
115128
mockP5Prototype.saveCanvas();
116-
await wait(100);
129+
await downloaded;
130+
117131
expect(document.createElement).toHaveBeenCalledTimes(1);
118132
expect(mockAnchorElement.click).toHaveBeenCalledTimes(1);
119133
assert.equal(mockAnchorElement.download, 'untitled.png');
120134
});
121135

122136
test('should download a jpg file I', async () => {
137+
const downloaded = downloadHappened();
123138
mockP5Prototype.saveCanvas('filename.jpg');
124-
await wait(100);
139+
await downloaded;
140+
125141
expect(document.createElement).toHaveBeenCalledTimes(1);
126142
expect(mockAnchorElement.click).toHaveBeenCalledTimes(1);
127143
assert.equal(mockAnchorElement.download, 'filename.jpg');
128144
});
129145

130146
test('should download a jpg file II', async () => {
147+
const downloaded = downloadHappened();
131148
mockP5Prototype.saveCanvas('filename', 'jpg');
132-
await wait(100);
149+
await downloaded;
150+
133151
expect(document.createElement).toHaveBeenCalledTimes(1);
134152
expect(mockAnchorElement.click).toHaveBeenCalledTimes(1);
135153
assert.equal(mockAnchorElement.download, 'filename.jpg');

‎test/unit/webgl/p5.Framebuffer.js‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ const mockAnchorElement = vi.mockObject({
66
download: null,
77
click: () => {}
88
});
9+
10+
let onDownload = null;
11+
mockAnchorElement.click.mockImplementation(() => {
12+
if(onDownload) onDownload();
13+
});
14+
15+
const downloadHappened = () =>
16+
new Promise(resolve => {
17+
onDownload = resolve;
18+
});
19+
920
const originalCreateElement = document.createElement;
1021
vi.spyOn(document, 'createElement').mockImplementation((...args) => {
1122
if (args[0] !== 'a') {
@@ -53,6 +64,10 @@ suite('p5.Framebuffer', function () {
5364
vi.clearAllMocks();
5465
});
5566

67+
beforeEach(function () {
68+
onDownload = null;
69+
});
70+
5671
suite('formats and channels', function () {
5772
function testWithConfiguration(
5873
version,
@@ -671,9 +686,10 @@ suite('p5.Framebuffer', function () {
671686
myp5.createCanvas(100, 100, myp5.WEBGL);
672687
const fbo = myp5.createFramebuffer();
673688
fbo.draw(() => myp5.background('red'));
674-
myp5.saveCanvas(fbo);
675689

676-
await new Promise(res => setTimeout(res, 500));
690+
const downloaded = downloadHappened();
691+
myp5.saveCanvas(fbo);
692+
await downloaded;
677693

678694
expect(document.createElement).toHaveBeenCalledWith('a');
679695
expect(mockAnchorElement.click).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)