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

Commit

Permalink
refactor: use @vue/test-utils flushPromises & refactor type definitio…
Browse files Browse the repository at this point in the history
…ns (#740)

* refactor(test): use @vue/test-utils flushPromises
* refactor: reduce any uses
* refactor: move type definition
  • Loading branch information
nogic1008 authored Feb 7, 2023
1 parent af13b2c commit fa090b4
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 66 deletions.
3 changes: 2 additions & 1 deletion __tests__/content/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { basename } from 'node:path'
import { load } from 'js-yaml'
import { describe, expect, test } from 'vitest'

import type { Software } from '~~/composables/useSoftwareData'
import { platforms, regionList } from '~~/composables/useSoftwareData'
import seriesList from '~~/content/series/.seriesList.json'
import { platforms, regionList, Software } from '~~/src/content'

const dirpath = './content/series'

Expand Down
2 changes: 1 addition & 1 deletion __tests__/content/songs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { basename } from 'node:path'
import { load } from 'js-yaml'
import { describe, expect, test } from 'vitest'

import type { Song } from '~~/composables/useSongData'
import seriesList from '~~/content/series/.seriesList.json'
import { Song } from '~~/src/content'

const dirpath = './content/songs'

Expand Down
2 changes: 1 addition & 1 deletion __tests__/pages/songs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('pages/songs/index.vue', () => {
bpm: 180,
series: ['1st-jp', '2nd']
}
]
] as Awaited<ReturnType<typeof useSongList>>['songs']['value']
const global = { plugins, stubs: { NuxtLink: RouterLinkStub, Series: true } }

beforeEach(() => {
Expand Down
25 changes: 14 additions & 11 deletions __tests__/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Oruga from '@oruga-ui/oruga-next'
import { bulmaConfig } from '@oruga-ui/theme-bulma'
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import { flushPromises, mount } from '@vue/test-utils'
import type { DefineComponent, Plugin } from 'vue'
import { defineComponent, h, Suspense } from 'vue'

import type { SoftwareParsedContent } from '~~/composables/useSoftwareData'
import type { SoftwareListData } from '~~/composables/useSoftwareList'
import type { SongParsedContent } from '~~/composables/useSongData'

export const plugins: (Plugin | [Plugin, ...any[]])[] = [[Oruga, bulmaConfig]]
export const plugins: (Plugin | [Plugin, ...unknown[]])[] = [
[Oruga, bulmaConfig]
]

export const mountAsync = async <T extends DefineComponent<any, any, any, any>>(
export const mountAsync = async <T extends DefineComponent<{}, {}, {}, any>>(
component: T,
options: Parameters<typeof mount<T>>[1]
) => {
Expand All @@ -33,7 +30,9 @@ export const mountAsync = async <T extends DefineComponent<any, any, any, any>>(
return wrapper
}

export const mockSoftware: SoftwareParsedContent = {
export const mockSoftware: Awaited<
ReturnType<typeof useSoftwareData>
>['software']['value'] = {
slug: '1st-jp',
title: 'Dance Dance Revolution',
platform: 'Play Station',
Expand All @@ -49,7 +48,9 @@ export const mockSoftware: SoftwareParsedContent = {
_id: 'content:series:1st-jp.md'
}

export const mockSoftwareList: SoftwareListData[] = [
export const mockSoftwareList: Awaited<
ReturnType<typeof useSoftwareList>
>['softwareList']['value'] = [
{
slug: '1st-jp',
title: 'Dance Dance Revolution',
Expand All @@ -66,7 +67,9 @@ export const mockSoftwareList: SoftwareListData[] = [
}
]

export const mockSong: SongParsedContent = {
export const mockSong: Awaited<
ReturnType<typeof useSongData>
>['song']['value'] = {
slug: 'butterfly',
name: 'butterfly',
artist: 'smile. dk',
Expand Down
2 changes: 1 addition & 1 deletion components/content/Charts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</template>

<script lang="ts" setup>
import type { Software } from '~~/src/content'
import type { Software } from '~~/composables/useSoftwareData'
interface Props {
difficulties: Software['difficulties']
Expand Down
2 changes: 1 addition & 1 deletion components/content/SeriesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</template>

<script lang="ts" setup>
import type { Song } from '~~/composables/useSongData'
import seriesList from '~~/content/series/.seriesList.json'
import type { Song } from '~~/src/content'
interface SeriesListProps {
series: Song['series']
Expand Down
25 changes: 21 additions & 4 deletions composables/useSoftwareData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
import type { Ref } from 'vue'

import type { Software } from '~~/src/content'
import type seriesList from '~~/content/series/.seriesList.json'

export type SoftwareParsedContent = ParsedContent & Software
export const regionList = ['JP', 'US', 'EU', 'None'] as const

export const platforms = [
'Play Station',
'Play Station 2',
'Dreamcast',
'GAME BOY COLOR',
'Wii'
] as const

export interface Software {
slug: keyof typeof seriesList
title: string
platform: (typeof platforms)[number]
region: (typeof regionList)[number]
launched: string
difficulties: { [key in number]: { name: string; class: string } }
}

export default async function (slug: string) {
const { data } = await useAsyncData(`/software/${slug}`, () =>
queryContent<SoftwareParsedContent>()
queryContent<Software & ParsedContent>()
.where({ _type: 'markdown', slug })
.findOne()
)

return { software: data as Ref<SoftwareParsedContent> }
return { software: data as Ref<Software & ParsedContent> }
}
13 changes: 9 additions & 4 deletions composables/useSoftwareList.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import type { Ref } from 'vue'

import type { Software } from '~~/src/content'
import type { Software } from '~~/composables/useSoftwareData'

const keys = ['slug', 'title', 'platform', 'region', 'launched'] as const
export type SoftwareListData = Pick<Software, (typeof keys)[number]>
export type SoftwareListData = Omit<Software, 'difficulties'>

export default async function () {
const { data } = await useAsyncData('/software', () =>
queryContent<Software>('series')
.sort({ launched: 1 })
.only([...keys])
.only<(keyof Software)[]>([
'slug',
'title',
'platform',
'region',
'launched'
])
.find()
)

Expand Down
14 changes: 11 additions & 3 deletions composables/useSongData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
import type { Ref } from 'vue'

import type { Song } from '~~/src/content'
import type seriesList from '~~/content/series/.seriesList.json'

export interface Song {
slug: string
name: string
artist: string
bpm: number | string
series: (keyof typeof seriesList)[]
}

export type SongParsedContent = ParsedContent & Song

export default async function (slug: string) {
const { data } = await useAsyncData(`/songs/${slug}`, () =>
queryContent<SongParsedContent>()
queryContent<ParsedContent & Song>()
.where({ _type: 'markdown', slug: { $eq: slug } })
.findOne()
)

return { song: data as Ref<SongParsedContent> }
return { song: data as Ref<ParsedContent & Song> }
}
9 changes: 6 additions & 3 deletions composables/useSongList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Ref } from 'vue'

import type { Song } from '~~/src/content'
import type { Song } from '~~/composables/useSongData'

export default async function (key: string, ...keys: (keyof Song)[]) {
export default async function <T extends keyof Song>(
key: string,
...keys: T[]
) {
const { data } = await useAsyncData(key, () =>
queryContent<Song>('songs').sort({ name: 1 }).only(keys).find()
)

return { songs: data as Ref<Pick<Song, (typeof keys)[number]>[]> }
return { songs: data as Ref<Pick<Song, T>[]> }
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-nuxt": "^4.0.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"flush-promises": "^1.0.2",
"js-yaml": "^4.1.0",
"jsdom": "^21.1.0",
"npm-run-all2": "^6.0.4",
Expand Down
3 changes: 1 addition & 2 deletions pages/songs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<OTableColumn v-slot="props" label="Series">
<div class="buttons">
<NuxtLink
v-for="slug in (props.row as Song).series"
v-for="slug in (props.row as (typeof songs[number])).series"
:key="slug"
class="button is-small"
:to="`/series/${slug}`"
Expand Down Expand Up @@ -45,7 +45,6 @@
<script lang="ts" setup>
import useSongList from '~~/composables/useSongList'
import seriesList from '~~/content/series/.seriesList.json'
import type { Song } from '~~/src/content'
useHead({ title: 'Song List' })
Expand Down
28 changes: 0 additions & 28 deletions src/content.ts

This file was deleted.

5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2894,11 +2894,6 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

flush-promises@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/flush-promises/-/flush-promises-1.0.2.tgz#4948fd58f15281fed79cbafc86293d5bb09b2ced"
integrity sha512-G0sYfLQERwKz4+4iOZYQEZVpOt9zQrlItIxQAAYAWpfby3gbHrx0osCHz5RLl/XoXevXk0xoN4hDFky/VV9TrA==

follow-redirects@^1.0.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
Expand Down

0 comments on commit fa090b4

Please sign in to comment.