Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API #636

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('<ForgotPasswordCard />', () => {
beforeEach(() => {
vi.mock('lib/api/Account', () => ({
Account: function Account() {
this.recoverCreate = mockSuccessAccountRecoverCreate
this.sendRecoveryEmail = mockSuccessAccountRecoverCreate
},
}))
})
Expand Down
10 changes: 5 additions & 5 deletions components/blocks/cards/LogInCard/LogInCard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import LogInCard from './LogInCard.vue'
import * as apiComposables from 'composables/api'

const token = 'jwt-token'
const mockSuccessAccountLoginCreate = vi.fn(() =>
const mockSuccessAccountLogin = vi.fn(() =>
Promise.resolve({ data: { token }, ok: true }),
)
const mockSuccessUsersMeList = vi.fn(() =>
const mockSuccessMe = vi.fn(() =>
Promise.resolve({
data: { id: 1, username: 'admin' },
ok: true,
Expand All @@ -22,12 +22,12 @@ describe('<LogInCard />', () => {
beforeEach(() => {
vi.mock('lib/api/Account', () => ({
Account: function Account() {
this.loginCreate = mockSuccessAccountLoginCreate
this.login = mockSuccessAccountLogin
},
}))
vi.mock('lib/api/Users', () => ({
Users: function Users() {
this.usersMeList = mockSuccessUsersMeList
this.me = mockSuccessMe
},
}))
})
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('<LogInCard />', () => {
await getByTestId(wrapper, 'login-button').trigger('click')

expect(useLoginUserSpy).toBeCalledTimes(1)
expect(mockSuccessAccountLoginCreate).toBeCalledTimes(1)
expect(mockSuccessAccountLogin).toBeCalledTimes(1)
})
})

Expand Down
4 changes: 2 additions & 2 deletions composables/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { default as useChangePassword } from './useChangePassword'
export { default as useConfirmAccount } from './useConfirmAccount'
export { default as useGetLeaderboardDetail } from './useGetLeaderboardDetail'
export { default as useGetUserDetail } from './useGetUserDetail'
export { default as useGetLeaderboardBySlug } from './useGetLeaderboardBySlug'
export { default as useGetUser } from './useGetUser'
export { default as useLoginUser } from './useLoginUser'
export { default as useLogoutUser } from './useLogoutUser'
export { default as useRecoverAccount } from './useRecoverAccount'
Expand Down
2 changes: 1 addition & 1 deletion composables/api/useChangePassword/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function useChangePassword(

// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
return await useApi<void>(
async () => await account.recoverCreate2(token, requestData),
async () => await account.changePassword(token, requestData),
{
onError,
onOkay,
Expand Down
8 changes: 4 additions & 4 deletions composables/api/useChangePassword/useChangePassword.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useChangePassword from '.'

const mockSuccessRecoverCreate2 = vi.fn(() => Promise.resolve({ ok: true }))
const mockSuccessChangePassword = vi.fn(() => Promise.resolve({ ok: true }))

describe('useChangePassword', () => {
describe('when everything is successful', () => {
Expand All @@ -10,14 +10,14 @@ describe('useChangePassword', () => {
it('changes the password for the user', async () => {
vi.mock('lib/api/Account', () => ({
Account: function Account() {
this.recoverCreate2 = mockSuccessRecoverCreate2
this.changePassword = mockSuccessChangePassword
},
}))

await useChangePassword(token, requestData)

expect(mockSuccessRecoverCreate2).toBeCalledTimes(1)
expect(mockSuccessRecoverCreate2).toBeCalledWith(token, requestData)
expect(mockSuccessChangePassword).toBeCalledTimes(1)
expect(mockSuccessChangePassword).toBeCalledWith(token, requestData)
})
})
})
2 changes: 1 addition & 1 deletion composables/api/useConfirmAccount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function useConfirmAccount(

// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
return await useApi<void>(
async () => await account.confirmUpdate(confirmationToken),
async () => await account.confirmAccount(confirmationToken),
{ onError, onOkay },
)
}
Expand Down
4 changes: 2 additions & 2 deletions composables/api/useConfirmAccount/useConfirmAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const mockSuccessAccountConfirmation = vi.fn(() =>
Promise.resolve({ ok: true }),
)

describe('useConfirmUser', () => {
describe('useConfirmAccount', () => {
describe('when everything is successful', () => {
const confirmationCode = '123'

it('creates a PUT request to confirm the account', async () => {
vi.mock('lib/api/Account', () => ({
Account: function Account() {
this.confirmUpdate = mockSuccessAccountConfirmation
this.confirmAccount = mockSuccessAccountConfirmation
},
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Leaderboards } from 'lib/api/Leaderboards'
import type { LeaderboardViewModel } from 'lib/api/data-contracts'
import { useRuntimeConfig } from '#imports'

export async function useGetLeaderboardDetail(
export default async function useGetLeaderboardBySlug(
leaderboardSlug: string,
opts: optionalParameters<LeaderboardViewModel> = {},
): Promise<ApiResponse<LeaderboardViewModel>> {
Expand All @@ -17,22 +17,24 @@ export async function useGetLeaderboardDetail(
categories: [],
id: -1,
name: '',
rules: '',
slug: '',
info: null,
createdAt: '',
updatedAt: null,
deletedAt: null,
})

const leaderboardClient = new Leaderboards({
baseUrl: useRuntimeConfig().public.backendBaseUrl,
})

return await useApi<LeaderboardViewModel>(
async () => await leaderboardClient.leaderboardsDetail2(leaderboardSlug),
async () =>
await leaderboardClient.getLeaderboardBySlug({ slug: leaderboardSlug }),
{
onError,
onOkay,
responseData,
},
)
}

export default useGetLeaderboardDetail
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import useGetLeaderboardBySlug from '.'

const mockSuccessGetLeaderboardBySlug = vi.fn(() =>
Promise.resolve({ ok: true }),
)

describe('useGetLeaderboardBySlug', () => {
describe('when everything is successful', () => {
const slug = 'pieces'

it('creates a GET request to fetch the leaderboard details', async () => {
vi.mock('lib/api/Leaderboards', () => ({
Leaderboards: function Leaderboards() {
this.getLeaderboardBySlug = mockSuccessGetLeaderboardBySlug
},
}))

await useGetLeaderboardBySlug(slug)

expect(mockSuccessGetLeaderboardBySlug).toBeCalledTimes(1)
expect(mockSuccessGetLeaderboardBySlug).toBeCalledWith({ slug })
})
})
})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Users } from 'lib/api/Users'
import type { UserViewModel } from 'lib/api/data-contracts'

export async function useGetUserDetail(
export default async function useGetUser(
userId: string,
opts: optionalParameters<UserViewModel> = {},
): Promise<ApiResponse<UserViewModel>> {
Expand All @@ -18,12 +18,10 @@ export async function useGetUserDetail(
})

return await useApi<UserViewModel>(
async () => await userClient.usersDetail(userId),
async () => await userClient.getUser(userId),
{
onError,
onOkay,
},
)
}

export default useGetUserDetail
22 changes: 22 additions & 0 deletions composables/api/useGetUser/useGetUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import useGetUser from '.'

const mockSuccessGetUser = vi.fn(() => Promise.resolve({ ok: true }))

describe('useGetUser', () => {
describe('when everything is successful', () => {
const userId = 'cd79f511-a952-48d5-a1c9-262449bc2e34'

it('creates a GET request to fetch the user details', async () => {
vi.mock('lib/api/Users', () => ({
Users: function Users() {
this.getUser = mockSuccessGetUser
},
}))

await useGetUser(userId)

expect(mockSuccessGetUser).toBeCalledTimes(1)
expect(mockSuccessGetUser).toBeCalledWith(userId)
})
})
})
22 changes: 0 additions & 22 deletions composables/api/useGetUserDetail/useGetUserDetail.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion composables/api/useLoginUser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function useLoginUser(
})

return await useApi<LoginResponse>(
async () => await account.loginCreate(requestData),
async () => await account.login(requestData),
{
onError,
onOkay: async (d: LoginResponse) => {
Expand Down
13 changes: 7 additions & 6 deletions composables/api/useLoginUser/useLoginUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { useSessionToken } from '#imports'
import useLoginUser from '.'

// const mockFailureUsersLoginCreate = vi.fn(() => Promise.resolve({ ok: false }))
const mockSuccessAccountLoginCreate = vi.fn(() =>
const mockSuccessAccountLogin = vi.fn(() =>
Promise.resolve({ data: { token: 'token' }, ok: true }),
)
const mockSuccessUsersMeList = vi.fn(() =>
const mockSuccessMe = vi.fn(() =>
Promise.resolve({
data: {
id: '05864eb1-540a-4b32-ad57-17871f2519f3',
role: 'Confirmed',
username: 'foo',
createdAt: '1984-01-01T00:00:00Z',
},
ok: true,
}),
Expand All @@ -31,19 +32,19 @@ describe('useLoginUser', () => {
it('creates a login session and returns the user information', async () => {
vi.mock('lib/api/Users', () => ({
Users: function Users() {
this.usersMeList = mockSuccessUsersMeList
this.me = mockSuccessMe
},
}))
vi.mock('lib/api/Account', () => ({
Account: function Account() {
this.loginCreate = mockSuccessAccountLoginCreate
this.login = mockSuccessAccountLogin
},
}))

await useLoginUser({ email, password }, { onOkay: onOkaySpy })

expect(mockSuccessAccountLoginCreate).toBeCalledTimes(1)
expect(mockSuccessAccountLoginCreate).toBeCalledWith({ email, password })
expect(mockSuccessAccountLogin).toBeCalledTimes(1)
expect(mockSuccessAccountLogin).toBeCalledWith({ email, password })

expect(onOkaySpy).toBeCalledTimes(1)
expect(useSessionToken().value).toBeTruthy()
Expand Down
3 changes: 2 additions & 1 deletion composables/api/useLogoutUser/useLogoutUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const user: UserViewModel = {
id: '575888bd-9702-41a9-9b75-fc87d785c22a',
role: 'Confirmed',
username: 'test',
createdAt: '1984-01-01T00:00:00Z',
}

afterEach(() => {
Expand All @@ -17,7 +18,7 @@ afterEach(() => {

describe('useLogoutUser', () => {
beforeEach(() => {
fetchMock.mockIf(/.*\/api\/[Uu]sers\/me/, () => JSON.stringify(user))
fetchMock.mockIf(/.*\/[Uu]ser\/me/, () => JSON.stringify(user))
useSessionToken().value = token
})

Expand Down
2 changes: 1 addition & 1 deletion composables/api/useRecoverAccount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function useRecoverAccount(

// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
return await useApi<void>(
async () => await account.recoverCreate(requestData),
async () => await account.sendRecoveryEmail(requestData),
{
onError,
onOkay,
Expand Down
8 changes: 4 additions & 4 deletions composables/api/useRecoverAccount/useRecoverAccount.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useRecoverAccount from '.'

const mockSuccessRecoverCreate = vi.fn(() => Promise.resolve({ ok: true }))
const mockSuccessRecover = vi.fn(() => Promise.resolve({ ok: true }))

describe('useRecoverAccount', () => {
describe('when everything is successful', () => {
Expand All @@ -9,14 +9,14 @@ describe('useRecoverAccount', () => {
it('kicks off the account recovery process', async () => {
vi.mock('lib/api/Account', () => ({
Account: function Account() {
this.recoverCreate = mockSuccessRecoverCreate
this.sendRecoveryEmail = mockSuccessRecover
},
}))

await useRecoverAccount(requestData)

expect(mockSuccessRecoverCreate).toBeCalledTimes(1)
expect(mockSuccessRecoverCreate).toBeCalledWith(requestData)
expect(mockSuccessRecover).toBeCalledTimes(1)
expect(mockSuccessRecover).toBeCalledWith(requestData)
})
})
})
2 changes: 1 addition & 1 deletion composables/api/useRegisterUser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function useRegisterUser(
})

return await useApi<UserViewModel>(
async () => await accountClient.registerCreate(requestData),
async () => await accountClient.register(requestData),
{
onError,
onOkay,
Expand Down
Loading