test: make tests order-independent

MariaDB seems to order the returned media objects in a different way,
making our tests fail.

This refactors the tests to be independent of the order of returned data.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-03-05 18:47:45 +01:00
parent c7e77d25a0
commit 9030488025
2 changed files with 35 additions and 17 deletions

View file

@ -71,20 +71,29 @@ describe('Me', () => {
expect(responseBefore.body).toHaveLength(0); expect(responseBefore.body).toHaveLength(0);
const testImage = await fs.readFile('test/public-api/fixtures/test.png'); const testImage = await fs.readFile('test/public-api/fixtures/test.png');
const url0 = await testSetup.mediaService.saveFile(testImage, user, note1); const imageUrls = [];
const url1 = await testSetup.mediaService.saveFile(testImage, user, note1); imageUrls.push(
const url2 = await testSetup.mediaService.saveFile(testImage, user, note2); await testSetup.mediaService.saveFile(testImage, user, note1),
const url3 = await testSetup.mediaService.saveFile(testImage, user, note2); );
imageUrls.push(
await testSetup.mediaService.saveFile(testImage, user, note1),
);
imageUrls.push(
await testSetup.mediaService.saveFile(testImage, user, note2),
);
imageUrls.push(
await testSetup.mediaService.saveFile(testImage, user, note2),
);
const response = await agent const response = await agent
.get('/api/private/me/media/') .get('/api/private/me/media/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body).toHaveLength(4); expect(response.body).toHaveLength(4);
expect(response.body[0].url).toEqual(url0); expect(imageUrls).toContain(response.body[0].url);
expect(response.body[1].url).toEqual(url1); expect(imageUrls).toContain(response.body[1].url);
expect(response.body[2].url).toEqual(url2); expect(imageUrls).toContain(response.body[2].url);
expect(response.body[3].url).toEqual(url3); expect(imageUrls).toContain(response.body[3].url);
const mediaUploads = await testSetup.mediaService.listUploadsByUser(user); const mediaUploads = await testSetup.mediaService.listUploadsByUser(user);
for (const upload of mediaUploads) { for (const upload of mediaUploads) {
await testSetup.mediaService.deleteFile(upload); await testSetup.mediaService.deleteFile(upload);

View file

@ -202,21 +202,30 @@ describe('Me', () => {
expect(response1.body).toHaveLength(0); expect(response1.body).toHaveLength(0);
const testImage = await fs.readFile('test/public-api/fixtures/test.png'); const testImage = await fs.readFile('test/public-api/fixtures/test.png');
const url0 = await testSetup.mediaService.saveFile(testImage, user, note1); const imageUrls = [];
const url1 = await testSetup.mediaService.saveFile(testImage, user, note1); imageUrls.push(
const url2 = await testSetup.mediaService.saveFile(testImage, user, note2); await testSetup.mediaService.saveFile(testImage, user, note1),
const url3 = await testSetup.mediaService.saveFile(testImage, user, note2); );
imageUrls.push(
await testSetup.mediaService.saveFile(testImage, user, note1),
);
imageUrls.push(
await testSetup.mediaService.saveFile(testImage, user, note2),
);
imageUrls.push(
await testSetup.mediaService.saveFile(testImage, user, note2),
);
const response = await request(httpServer) const response = await request(httpServer)
.get('/api/v2/me/media/') .get('/api/v2/me/media/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body).toHaveLength(4); expect(response.body).toHaveLength(4);
expect(response.body[0].url).toEqual(url0); expect(imageUrls).toContain(response.body[0].url);
expect(response.body[1].url).toEqual(url1); expect(imageUrls).toContain(response.body[1].url);
expect(response.body[2].url).toEqual(url2); expect(imageUrls).toContain(response.body[2].url);
expect(response.body[3].url).toEqual(url3); expect(imageUrls).toContain(response.body[3].url);
for (const fileUrl of [url0, url1, url2, url3]) { for (const fileUrl of imageUrls) {
const fileName = fileUrl.replace('/uploads/', ''); const fileName = fileUrl.replace('/uploads/', '');
// delete the file afterwards // delete the file afterwards
await fs.unlink(join(uploadPath, fileName)); await fs.unlink(join(uploadPath, fileName));