Skip to content

Commit

Permalink
WIP: add pr changes to view leaderboards
Browse files Browse the repository at this point in the history
- update getLeaderboards -> listLeaderboards
- make default leaderboard returns use info: ''
- use correct components in some tests
  • Loading branch information
Eein committed Nov 14, 2024
1 parent 35018ba commit c43f14d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/blocks/Landing/Landing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import LandingLeaderboards from './LandingLeaderboards.vue'
const mockSuccessGetLeaderboards = vi.fn(() => Promise.resolve({ ok: true }))
vi.mock('lib/api/Leaderboards', () => ({
Leaderboards: function Leaderboards() {
this.getLeaderboards = mockSuccessGetLeaderboards
this.listLeaderboards = mockSuccessGetLeaderboards
},
}))

Expand Down
8 changes: 2 additions & 6 deletions components/blocks/Landing/LandingLeaderboards.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mountSuspended } from '@nuxt/test-utils/runtime'
import Landing from './LandingLeaderboards.vue'
import LandingLeaderboards from './LandingLeaderboards.vue'
import type { LeaderboardViewModel } from '~/lib/api/data-contracts'

const games: LeaderboardViewModel[] = [
Expand Down Expand Up @@ -36,12 +36,8 @@ const games: LeaderboardViewModel[] = [
]

describe('LandingLeaderboards Component', () => {
afterEach(() => {
vi.restoreAllMocks()
})

it('should render without crashing', async () => {
const wrapper = await mountSuspended(Landing, {
const wrapper = await mountSuspended(LandingLeaderboards, {
props: {
leaderboards: games,
},
Expand Down
2 changes: 1 addition & 1 deletion composables/api/useGetLeaderboardBySlug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function useGetLeaderboardBySlug(
id: -1,
name: '',
slug: '',
info: null,
info: '',
createdAt: '',
updatedAt: null,
deletedAt: null,
Expand Down
4 changes: 2 additions & 2 deletions composables/api/useGetLeaderboards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function useGetLeaderboards(
id: -1,
name: '',
slug: '',
info: null,
info: '',
createdAt: '',
updatedAt: null,
deletedAt: null,
Expand All @@ -30,7 +30,7 @@ export default async function useGetLeaderboards(
})

return await useApi<LeaderboardViewModel[]>(
async () => await leaderboardClient.getLeaderboards({}),
async () => await leaderboardClient.listLeaderboards({}),
{
onError,
onOkay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('useGetLeaderboard', () => {
it('creates a GET request to fetch the a list of leaderboards', async () => {
vi.mock('lib/api/Leaderboards', () => ({
Leaderboards: function Leaderboards() {
this.getLeaderboards = mockSuccessGetLeaderboards
this.listLeaderboards = mockSuccessGetLeaderboards
},
}))

Expand Down
51 changes: 51 additions & 0 deletions pages/game/[slug].test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { mountSuspended } from '@nuxt/test-utils/runtime'
import useGetLeaderboardBySlug from '~/composables/api/useGetLeaderboardBySlug/index'
import gamePage from 'pages/game/[slug].vue'

import type { LeaderboardViewModel } from '~/lib/api/data-contracts'

const leaderboard: LeaderboardViewModel = {
id: 1,
name: 'Getting Over It With Bennet Foddy',
slug: 'slug-2',
info: '',
createdAt: '2024-11-02T22:11:08+0000',
updatedAt: '2024-11-02T22:11:08+0000',
deletedAt: null,
categories: [],
}

vi.mock('composables/api/useGetLeaderboardBySlug')

describe('/game/:slug', () => {
it('should render without crashing with a valid leaderboard', async () => {
vi.mocked(useGetLeaderboardBySlug).mockResolvedValue({
error: null,
loading: false,
data: leaderboard,
errors: null,
})
const wrapper = await mountSuspended(gamePage, {
route: '/game/validslug',
})

expect(wrapper.text()).toContain(leaderboard.name)
expect(wrapper.isVisible()).toBe(true)
})

it('should render 404 if leaderboard status is 404', async () => {
vi.mocked(useGetLeaderboardBySlug).mockResolvedValue({
error: { status: 404 },
loading: false,
data: leaderboard,
errors: null,
})

// this correctly does not render the component when error status is 404
const wrapper = await mountSuspended(gamePage, {
route: '/game/invalidslug',
})
expect(wrapper.text()).not.toContain(leaderboard.name)
// Ideally we'd check for the error
})
})
5 changes: 3 additions & 2 deletions pages/game/[slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { useGetLeaderboardBySlug } from '~/composables/api'
import LeaderboardInfo from '~/components/blocks/LeaderboardInfo/LeaderboardInfo.vue'
const route = useRoute()
const leaderboardSlug = route.params.slug as string
const leaderboardDetail = await useGetLeaderboardBySlug(leaderboardSlug)
if (leaderboardDetail?.error?.status == 404) {
if (leaderboardDetail?.error?.status === 404) {
throw createError({
statusCode: 404,
statusMessage: 'Page Not Found',
Expand All @@ -18,7 +19,7 @@ if (leaderboardDetail?.error?.status == 404) {

<template>
<div>
<Loader v-if="leaderboardDetail.loading"></Loader>
<Loader v-if="leaderboardDetail?.loading"></Loader>
<LeaderboardInfo v-else :leaderboard="leaderboardDetail?.data!" />
</div>
</template>

0 comments on commit c43f14d

Please sign in to comment.