Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

Commit d4b19c8

Browse files
committed
test: add transform test
1 parent b0be7a3 commit d4b19c8

File tree

9 files changed

+247
-34
lines changed

9 files changed

+247
-34
lines changed

__tests__/components/SearchBox.test.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ import { describe, expect, test, vi } from 'vitest'
22
import { ref } from 'vue'
33

44
import SearchBox, { SearchSongResult } from '~~/components/SearchBox.vue'
5+
import boysFirst from '~~/content/1st-jp/boys-smiledk.json'
6+
import butterfly from '~~/content/1st-jp/butterfly.json'
7+
import littleBitch from '~~/content/1st-jp/little-bitch.json'
8+
import myFire from '~~/content/1st-jp/my-fire.json'
9+
import boysSecond from '~~/content/2nd/boys-smiledk.json'
510

611
import { mountAsync, plugins } from '../test-utils'
712

13+
const rawSongData = [boysFirst, butterfly, littleBitch, myFire, boysSecond].map(
14+
(d) => ({ slug: d.slug, name: d.name, artist: d.artist })
15+
)
16+
817
describe('components/SearchBox.vue', () => {
9-
const songData: SearchSongResult[] = [
10-
{ slug: 'boys-smiledk', name: 'BOYS', artist: 'smile. dk' },
11-
{ slug: 'butterfly', name: 'butterfly', artist: 'smile. dk' },
12-
{ slug: 'little-bitch', name: 'LITTLE BITCH', artist: 'THE SPECIALS' },
13-
{ slug: 'my-fire', name: 'MY FIRE', artist: 'X-TREME' }
14-
]
18+
const songs: SearchSongResult[] = [...rawSongData]
19+
songs.pop()
1520

1621
describe('snapshot test', () => {
1722
test('{ isLoading: true, term: "" } renders loading state', async () => {
@@ -40,10 +45,27 @@ describe('components/SearchBox.vue', () => {
4045
})
4146
})
4247

48+
describe('useLazyAsyncData()', () => {
49+
test('calls with transform option', async () => {
50+
// Arrange
51+
const data = ref([])
52+
const pending = ref(false)
53+
vi.mocked(useLazyAsyncData).mockReset()
54+
vi.mocked(useLazyAsyncData).mockResolvedValue({ data, pending } as any)
55+
56+
// Act
57+
await mountAsync(SearchBox, { global: { plugins } })
58+
const transform = vi.mocked(useLazyAsyncData).mock.calls[0][2]?.transform!
59+
60+
// Assert
61+
expect(transform(rawSongData)).toStrictEqual(songs)
62+
})
63+
})
64+
4365
describe('term', () => {
4466
test('@change renders autocomplete', async () => {
4567
// Arrange
46-
const data = ref(songData)
68+
const data = ref(songs)
4769
const pending = ref(false)
4870
vi.mocked(useLazyAsyncData).mockResolvedValue({ data, pending } as any)
4971

@@ -59,7 +81,7 @@ describe('components/SearchBox.vue', () => {
5981
describe('autocomplete', () => {
6082
test('@select calls router.push("/songs/{id}")', async () => {
6183
// Arrange
62-
const data = ref(songData)
84+
const data = ref(songs)
6385
const pending = ref(false)
6486
vi.mocked(useLazyAsyncData).mockResolvedValue({ data, pending } as any)
6587
const push = vi.fn()
@@ -71,7 +93,7 @@ describe('components/SearchBox.vue', () => {
7193
await wrapper.find('.dropdown-item').trigger('click')
7294

7395
// Assert
74-
expect(push).toBeCalledWith(`/songs/${songData[0].slug}`)
96+
expect(push).toBeCalledWith(`/songs/${songs[0].slug}`)
7597
})
7698
})
7799
})

__tests__/content/json.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('/content', async () => {
3434

3535
// Assert
3636
expect(validate(jsonObject)).toBeTruthy()
37+
expect(`${jsonObject.slug}.json`).toBe(fileName)
3738
expect(jsonObject.series).toBe(series)
3839
})
3940
})

__tests__/pages/series/[id].test.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ import { RouterLinkStub } from '@vue/test-utils'
22
import { beforeAll, describe, expect, test, vi } from 'vitest'
33
import { ref } from 'vue'
44

5+
import butterfly from '~~/content/1st-jp/butterfly.json'
6+
import paranoia from '~~/content/1st-jp/paranoia.json'
57
import SeriesDetail from '~~/pages/series/[id].vue'
68

79
import { mountAsync, plugins } from '../../test-utils'
810

11+
const rawSongData = [butterfly, paranoia].map((d) => ({
12+
slug: d.slug,
13+
name: d.name,
14+
artist: d.artist,
15+
bpm: d.bpm,
16+
charts: d.charts
17+
}))
18+
919
describe('pages/series/[id].vue', () => {
1020
const software = {
1121
slug: '1st-jp',
@@ -22,11 +32,10 @@ describe('pages/series/[id].vue', () => {
2232
}
2333
const songs = [
2434
{
25-
slug: 'butterfly',
26-
series: '1st-jp',
27-
name: 'butterfly',
28-
artist: 'smile. dk',
29-
bpm: 135,
35+
slug: butterfly.slug,
36+
name: butterfly.name,
37+
artist: butterfly.artist,
38+
bpm: butterfly.bpm,
3039
charts: {
3140
SINGLE: [
3241
{ difficulty: 5, level: 3, name: 'NORMAL', color: 'is-basic' },
@@ -37,14 +46,20 @@ describe('pages/series/[id].vue', () => {
3746
DOUBLE: [
3847
{ difficulty: 1, level: 4, name: 'BASIC', color: 'is-basic' },
3948
{ difficulty: 2, level: 6, name: 'ANOTHER', color: 'is-difficult' }
49+
],
50+
COUPLE: [
51+
{ difficulty: 5, level: 3, name: 'NORMAL', color: 'is-basic' },
52+
{ difficulty: 1, level: 3, name: 'BASIC', color: 'is-basic' },
53+
{ difficulty: 2, level: 4, name: 'ANOTHER', color: 'is-difficult' },
54+
{ difficulty: 3, level: 6, name: 'MANIAC', color: 'is-expert' }
4055
]
4156
}
4257
},
4358
{
44-
slug: 'paranoia',
45-
name: 'PARANOiA',
46-
artist: '180',
47-
bpm: 180,
59+
slug: paranoia.slug,
60+
name: paranoia.name,
61+
artist: paranoia.artist,
62+
bpm: paranoia.bpm,
4863
charts: {
4964
SINGLE: [
5065
{ difficulty: 1, level: 6, name: 'BASIC', color: 'is-basic' },
@@ -54,6 +69,11 @@ describe('pages/series/[id].vue', () => {
5469
DOUBLE: [
5570
{ difficulty: 1, level: 7, name: 'BASIC', color: 'is-basic' },
5671
{ difficulty: 2, level: 8, name: 'ANOTHER', color: 'is-difficult' }
72+
],
73+
COUPLE: [
74+
{ difficulty: 1, level: 6, name: 'BASIC', color: 'is-basic' },
75+
{ difficulty: 2, level: 7, name: 'ANOTHER', color: 'is-difficult' },
76+
{ difficulty: 3, level: 8, name: 'MANIAC', color: 'is-expert' }
5777
]
5878
}
5979
}
@@ -102,4 +122,26 @@ describe('pages/series/[id].vue', () => {
102122
expect(wrapper.element).toMatchSnapshot()
103123
})
104124
})
125+
126+
describe('useAsyncData()', () => {
127+
test('callse with transform option', async () => {
128+
// Arrange
129+
vi.mocked(useAsyncData).mockReset()
130+
vi.mocked(useAsyncData).mockImplementation(
131+
(path) =>
132+
Promise.resolve(
133+
path.endsWith('songs')
134+
? { data: ref([]), pending: ref(true) }
135+
: { data: ref(software) }
136+
) as any
137+
)
138+
139+
// Act
140+
await mountAsync(SeriesDetail, { global })
141+
const transform = vi.mocked(useAsyncData).mock.calls[1][2]?.transform!
142+
143+
// Assert
144+
expect(transform(rawSongData)).toStrictEqual(songs)
145+
})
146+
})
105147
})

__tests__/pages/series/__snapshots__/[id].test.ts.snap

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ exports[`pages/series/[id].vue > snapshot test > renders song info 1`] = `
221221
>
222222
DOUBLE
223223
</span>
224+
<span
225+
data-id="6"
226+
>
227+
COUPLE
228+
</span>
224229
225230
226231
</div>
@@ -339,6 +344,26 @@ exports[`pages/series/[id].vue > snapshot test > renders song info 1`] = `
339344
</span>
340345
</span>
341346
</th>
347+
<th
348+
class=""
349+
draggable="false"
350+
>
351+
<span>
352+
COUPLE
353+
<span
354+
class="th-wrap sort-icon"
355+
style="display: none;"
356+
>
357+
<span
358+
class="icon is-small"
359+
>
360+
<i
361+
class="mdi mdi-arrow-up"
362+
/>
363+
</span>
364+
</span>
365+
</span>
366+
</th>
342367
343368
<!--v-if-->
344369
</tr>
@@ -440,6 +465,42 @@ exports[`pages/series/[id].vue > snapshot test > renders song info 1`] = `
440465
441466
</div>
442467
</td>
468+
<td
469+
class=""
470+
data-label="COUPLE"
471+
scoped=""
472+
>
473+
<div
474+
class="tags has-addons"
475+
>
476+
477+
<span
478+
class="tag is-basic"
479+
title="NORMAL"
480+
>
481+
3
482+
</span>
483+
<span
484+
class="tag is-basic"
485+
title="BASIC"
486+
>
487+
3
488+
</span>
489+
<span
490+
class="tag is-difficult"
491+
title="ANOTHER"
492+
>
493+
4
494+
</span>
495+
<span
496+
class="tag is-expert"
497+
title="MANIAC"
498+
>
499+
6
500+
</span>
501+
502+
</div>
503+
</td>
443504
444505
<!--v-if-->
445506
</tr>
@@ -535,6 +596,36 @@ exports[`pages/series/[id].vue > snapshot test > renders song info 1`] = `
535596
536597
</div>
537598
</td>
599+
<td
600+
class=""
601+
data-label="COUPLE"
602+
scoped=""
603+
>
604+
<div
605+
class="tags has-addons"
606+
>
607+
608+
<span
609+
class="tag is-basic"
610+
title="BASIC"
611+
>
612+
6
613+
</span>
614+
<span
615+
class="tag is-difficult"
616+
title="ANOTHER"
617+
>
618+
7
619+
</span>
620+
<span
621+
class="tag is-expert"
622+
title="MANIAC"
623+
>
624+
8
625+
</span>
626+
627+
</div>
628+
</td>
538629
539630
<!--v-if-->
540631
</tr>

__tests__/pages/songs/[id].test.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@ import { RouterLinkStub } from '@vue/test-utils'
22
import { beforeAll, describe, expect, test, vi } from 'vitest'
33
import { ref } from 'vue'
44

5+
import first from '~~/content/1st-jp/trip-machine.json'
6+
import third from '~~/content/3rd/trip-machine.json'
57
import SongDetail from '~~/pages/songs/[id].vue'
68

79
import { mountAsync, plugins } from '../../test-utils'
810

11+
const rawSongData = [first, third].map((d) => ({
12+
series: d.series,
13+
name: d.name,
14+
artist: d.artist,
15+
bpm: d.bpm,
16+
charts: d.charts
17+
}))
18+
919
describe('pages/songs/[id].vue', () => {
1020
const song = {
11-
name: 'TRIP MACHINE',
12-
artist: 'DE-SIRE',
13-
bpm: 160,
14-
series: ['1st-jp', '3rd'],
21+
name: first.name,
22+
artist: first.artist,
23+
bpm: first.bpm,
24+
series: [first.series, third.series],
1525
charts: [
1626
{ playStyle: 'SINGLE', difficulty: 1, levels: { '1st-jp': 6, '3rd': 6 } },
1727
{ playStyle: 'SINGLE', difficulty: 2, levels: { '1st-jp': 7, '3rd': 7 } },
@@ -33,6 +43,7 @@ describe('pages/songs/[id].vue', () => {
3343
vi.mocked(useRoute).mockReturnValue({
3444
params: { id: 'trip-machine' }
3545
} as any)
46+
vi.mocked(useAsyncData).mockReset()
3647
vi.mocked(useAsyncData).mockResolvedValue({ data: ref(song) } as any)
3748
})
3849

@@ -42,4 +53,15 @@ describe('pages/songs/[id].vue', () => {
4253
expect(wrapper.element).toMatchSnapshot()
4354
})
4455
})
56+
57+
describe('useAsyncData()', () => {
58+
test('calls with transform option', async () => {
59+
// Arrange - Act
60+
await mountAsync(SongDetail, { global })
61+
const transform = vi.mocked(useAsyncData).mock.calls[0][2]?.transform!
62+
63+
// Assert
64+
expect(transform(rawSongData)).toStrictEqual(song)
65+
})
66+
})
4567
})

0 commit comments

Comments
 (0)