From 6aa609970fe5774929f294ed1bf59258152fbaad Mon Sep 17 00:00:00 2001 From: wvffle Date: Tue, 20 Feb 2024 20:52:29 +0000 Subject: [PATCH] fix(tests): don't wait arbitrary time Part-of: --- front/package.json | 1 - .../specs/composables/audio/tracks.test.ts | 43 ++++++++++++------- front/test/specs/views/admin/library.test.ts | 14 +----- front/test/utils.ts | 1 - front/yarn.lock | 13 ------ 5 files changed, 29 insertions(+), 43 deletions(-) delete mode 100644 front/test/utils.ts diff --git a/front/package.json b/front/package.json index dadee7f33..41ec1c14f 100644 --- a/front/package.json +++ b/front/package.json @@ -79,7 +79,6 @@ "@vue/eslint-config-typescript": "12.0.0", "@vue/test-utils": "2.2.7", "@vue/tsconfig": "0.1.3", - "axios-mock-adapter": "1.21.4", "cypress": "13.6.4", "eslint": "8.30.0", "eslint-config-standard": "17.0.0", diff --git a/front/test/specs/composables/audio/tracks.test.ts b/front/test/specs/composables/audio/tracks.test.ts index 5663aa7bb..7ae65a06b 100644 --- a/front/test/specs/composables/audio/tracks.test.ts +++ b/front/test/specs/composables/audio/tracks.test.ts @@ -1,7 +1,7 @@ import { LRUCache } from 'lru-cache' import { currentIndex, useQueue } from '~/composables/audio/queue' import { useTracks } from '~/composables/audio/tracks' -import { sleep } from '?/utils' +import { isEqual } from 'lodash-es' import type { Sound } from '~/api/player' import type { Track } from '~/types' @@ -20,6 +20,11 @@ const createTrack = (() => { return { id: createTrack.id++, uploads: [] } as any as Track }) +const waitUntilCacheUpdated = async () => { + const keys = [...cache.rkeys()] + return vi.waitUntil(() => !isEqual(keys, [...cache.rkeys()]), { interval: 5 }) +} + beforeAll(() => { const { initialize } = useTracks() initialize() @@ -48,18 +53,18 @@ describe('cache', () => { it('caches next track after 100ms', async () => { expect(cache.size).toBe(1) - await sleep(110) + await waitUntilCacheUpdated() expect(cache.size).toBe(2) }) it('preserves previous track in cache, when next track is playing', async () => { expect(cache.size).toBe(1) - await sleep(110) + await waitUntilCacheUpdated() expect(cache.size).toBe(2) currentIndex.value += 1 - await sleep(110) + await waitUntilCacheUpdated() expect(cache.size).toBe(3) }) @@ -67,35 +72,38 @@ describe('cache', () => { expect(cache.size).toBe(1) const [[firstCachedId]] = cache.dump() - await sleep(110) + await waitUntilCacheUpdated() expect(cache.size).toBe(2) currentIndex.value += 1 - await sleep(110) + await waitUntilCacheUpdated() expect(cache.size).toBe(3) currentIndex.value += 1 - await sleep(110) + await waitUntilCacheUpdated() expect(cache.size).toBe(3) expect(cache.dump().map(([id]) => id)).not.toContain(firstCachedId) }) it('jumping around behaves correctly', async () => { currentIndex.value = 2 - await sleep(110) + // NOTE: waitUntilCacheUpdated() returns when first cache update is found + // That's why we need to call it twice after skipping the track + await waitUntilCacheUpdated() + await waitUntilCacheUpdated() expect([...cache.rkeys()]).toEqual([0, 2, 3]) currentIndex.value = 3 - await sleep(110) + await waitUntilCacheUpdated() expect([...cache.rkeys()]).toEqual([2, 3, 4]) // We change to the first song currentIndex.value = 0 - await sleep(0) // Wait until next macro task + await waitUntilCacheUpdated() expect([...cache.rkeys()]).toEqual([3, 4, 0]) // Now the next song should be enqueued - await sleep(110) + await waitUntilCacheUpdated() expect([...cache.rkeys()]).toEqual([4, 0, 1]) }) @@ -103,24 +111,27 @@ describe('cache', () => { // NOTE: We always want to have tracks 0, 1, 2 in the cache beforeEach(async () => { currentIndex.value += 1 - await sleep(110) + // NOTE: waitUntilCacheUpdated() returns when first cache update is found + // That's why we need to call it twice after skipping the track + await waitUntilCacheUpdated() + await waitUntilCacheUpdated() expect(cache.size).toBe(3) }) it('enqueueing track as next adds it to the cache', async () => { enqueueAt(currentIndex.value + 1, createTrack()) // id: 5 - await sleep(210) + await waitUntilCacheUpdated() const newIds = [...cache.rkeys()] expect(newIds).toEqual([2, 1, 5]) }) it('edge case: enqueueing track as next multiple times does not remove dispose current track', async () => { enqueueAt(currentIndex.value + 1, createTrack()) // id: 5 - await sleep(210) + await waitUntilCacheUpdated() enqueueAt(currentIndex.value + 1, createTrack()) // id: 6 - await sleep(210) + await waitUntilCacheUpdated() enqueueAt(currentIndex.value + 1, createTrack()) // id: 7 - await sleep(210) + await waitUntilCacheUpdated() const newIds = [...cache.rkeys()] expect(newIds).toEqual([6, 1, 7]) }) diff --git a/front/test/specs/views/admin/library.test.ts b/front/test/specs/views/admin/library.test.ts index 8e8583afa..dca3aaa2f 100644 --- a/front/test/specs/views/admin/library.test.ts +++ b/front/test/specs/views/admin/library.test.ts @@ -3,25 +3,15 @@ import AlbumDetail from '~/views/admin/library/AlbumDetail.vue' import SanitizedHtml from '~/components/SanitizedHtml.vue' import HumanDate from '~/components/common/HumanDate.vue' -import MockAdapter from 'axios-mock-adapter' -import axios from 'axios' - import { shallowMount } from '@vue/test-utils' -import { sleep } from '?/utils' +import { vi } from 'vitest' import router from '~/router' import store from '~/store' -const axiosMock = new MockAdapter(axios) - describe('views/admin/library', () => { describe('Album details', () => { it('displays default cover', async () => { - const album = { cover: null, artist: { id: 1 }, title: 'dummy', id: 1, creation_date: '2020-01-01' } - - axiosMock.onGet('manage/library/albums/1/').reply(200, album) - axiosMock.onGet('manage/library/albums/1/stats/').reply(200, {}) - const wrapper = shallowMount(AlbumDetail, { props: { id: 1 }, directives: { @@ -35,7 +25,7 @@ describe('views/admin/library', () => { } }) - await sleep() + await vi.waitUntil(() => wrapper.find('img').exists()) expect(wrapper.find('img').attributes('src')).to.include('default-cover') }) }) diff --git a/front/test/utils.ts b/front/test/utils.ts deleted file mode 100644 index febf34fdf..000000000 --- a/front/test/utils.ts +++ /dev/null @@ -1 +0,0 @@ -export const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)) diff --git a/front/yarn.lock b/front/yarn.lock index 66c240cda..1c8dfd5ea 100644 --- a/front/yarn.lock +++ b/front/yarn.lock @@ -3200,14 +3200,6 @@ axios-auth-refresh@3.3.6: resolved "https://registry.yarnpkg.com/axios-auth-refresh/-/axios-auth-refresh-3.3.6.tgz#a879f6296a889d6616e51069c2a8135b697966e7" integrity sha512-2CeBUce/SxIfFxow5/n8vApJ97yYF6qoV4gh1UrswT7aEOnlOdBLxxyhOI4IaxGs6BY0l8YujU2jlc4aCmK17Q== -axios-mock-adapter@1.21.4: - version "1.21.4" - resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.21.4.tgz#ced09b54b245b338422e3af425ae529bfa26e051" - integrity sha512-ztnENm28ONAKeRXC/6SUW6pcsaXbThKq93MRDRAA47LYTzrGSDoO/DCr1NHz7jApEl95DrBoGPvZ0r9xtSbjqw== - dependencies: - fast-deep-equal "^3.1.3" - is-buffer "^2.0.5" - axios@1.2.3: version "1.2.3" resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.3.tgz#31a3d824c0ebf754a004b585e5f04a5f87e6c4ff" @@ -5078,11 +5070,6 @@ is-boolean-object@^1.1.0: call-bind "^1.0.2" has-tostringtag "^1.0.0" -is-buffer@^2.0.5: - version "2.0.5" - resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.5.tgz#ebc252e400d22ff8d77fa09888821a24a658c191" - integrity sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ== - is-builtin-module@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-3.2.1.tgz#f03271717d8654cfcaf07ab0463faa3571581169"