Skip to content

Commit

Permalink
add fav api impl test
Browse files Browse the repository at this point in the history
  • Loading branch information
cglotr committed Jun 20, 2023
1 parent 21b8113 commit 6b1b061
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 9 deletions.
66 changes: 66 additions & 0 deletions __tests__/apis/fav_api_impl_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { FavApiImpl } from "../../src/apis/fav_api_impl"
import { server } from '../../mocks/server'

describe('FavApiImpl', () => {
beforeAll(() => server.listen())

afterEach(() => server.resetHandlers())

afterAll(() => server.close())

describe('valid token', () => {
const favApiImpl = new FavApiImpl('valid_token')

test('add', async () => {
const actual = await favApiImpl.add(1, 1)
const expected = [
{
id: 1,
surah: 1,
verse: 1
}
]
expect(actual).toEqual(expected)
})

test('remove', async () => {
const actual = await favApiImpl.remove(1)
expect(actual).toEqual([])
})

test('list', async () => {
const actual = await favApiImpl.list()
expect(actual).toEqual([
{
id: 1,
surah: 1,
verse: 1
},
{
id: 2,
surah: 1,
verse: 2
}
])
})
})

describe('invalid token', () => {
const favApiImpl = new FavApiImpl('invalid_token')

test('add', async () => {
const actual = await favApiImpl.add(1, 1)
expect(actual).toEqual([])
})

test('remove', async () => {
const actual = await favApiImpl.remove(1)
expect(actual).toEqual([])
})

test('list', async () => {
const actual = await favApiImpl.list()
expect(actual).toEqual([])
})
})
})
89 changes: 89 additions & 0 deletions mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { rest } from 'msw'

const VALID_TOKEN = 'valid_token'

const baseUrl = process.env.NEXT_PUBLIC_BASE_API

export const handlers = [
Expand Down Expand Up @@ -168,5 +170,92 @@ export const handlers = [
}
}),
)
}),
rest.post(`${baseUrl}/fav`, async (req, res, ctx) => {
const token = req.headers.get('x-access-token')

if (token == VALID_TOKEN) {
const json = await req.json()
return res(
ctx.status(200),
ctx.json({
data: {
favorites: [
{
id: 1,
surah: json.surah,
verse: json.verse,
}
]
}
}),
)
} else {
return res(
ctx.status(401),
ctx.json({
data: {
error: 'something went wrong'
}
}),
)
}
}),
rest.post(`${baseUrl}/fav/remove`, async (req, res, ctx) => {
const token = req.headers.get('x-access-token')

if (token == VALID_TOKEN) {
return res(
ctx.status(200),
ctx.json({
data: {
favorites: []
}
}),
)
} else {
return res(
ctx.status(401),
ctx.json({
data: {
error: 'something went wrong'
}
}),
)
}
}),
rest.get(`${baseUrl}/fav`, async (req, res, ctx) => {
const token = req.headers.get('x-access-token')

if (token == VALID_TOKEN) {
return res(
ctx.status(200),
ctx.json({
data: {
favorites: [
{
id: 1,
surah: 1,
verse: 1
},
{
id: 2,
surah: 1,
verse: 2
}
]
}
}),
)
} else {
return res(
ctx.status(401),
ctx.json({
data: {
error: 'something went wrong'
}
}),
)
}
})
]
10 changes: 1 addition & 9 deletions src/apis/fav_api_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export class FavApiImpl implements FavApi {
}

async add(surah: number, verse: number): Promise<Fav[]> {
if (!this.token) {
return []
}

const response = await fetch(getUrl("/fav"), {
method: 'POST',
body: JSON.stringify({
Expand All @@ -29,10 +25,6 @@ export class FavApiImpl implements FavApi {
}

async remove(id: number): Promise<Fav[]> {
if (!this.token) {
return []
}

const response = await fetch(getUrl("/fav/remove"), {
method: 'POST',
body: JSON.stringify({
Expand All @@ -58,7 +50,7 @@ export class FavApiImpl implements FavApi {
}

private async toFavs(response: Response): Promise<Fav[]> {
if (!this.token) {
if (response.status != 200) {
return []
}

Expand Down

0 comments on commit 6b1b061

Please sign in to comment.