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

ci(coverage): added more tests #6

Merged
merged 3 commits into from
Oct 24, 2023
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
16 changes: 8 additions & 8 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import './globals.css'
import classNames from 'classnames'
import Layout from '@/pkg/ui/views/Layout'
import Providers from '@/pkg/ui/Providers'

const inter = Inter({ subsets: ['latin'] })
Expand All @@ -20,13 +19,14 @@ export default function RootLayout({
return (
<html lang="en">
<body
className={classNames({
'bg-gray-200 min-h-screen': true
})}
>
<Providers>
{children}
</Providers>
className={
inter.className +
' ' +
classNames({
'bg-gray-200 min-h-screen': true,
})
}>
<Providers>{children}</Providers>
</body>
</html>
)
Expand Down
17 changes: 17 additions & 0 deletions src/app/page.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it, vi } from 'vitest'
import Home from './page'

vi.mock('next/navigation')
describe('Home()', () => {
it('should redirect page to /dashboard', async () => {
const next = await import('next/navigation')
next.redirect = (
await vi.importActual<typeof import('next/navigation')>('next/navigation')
).redirect

next.redirect = vi.fn()

Home()
expect(next.redirect).toBeCalledWith('/dashboard')
})
})
49 changes: 49 additions & 0 deletions src/pkg/github/api/delete.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fetchMock from 'fetch-mock'
import { createMocks } from 'node-mocks-http'
import { afterEach, describe, expect, it, vi } from 'vitest'
import deleteRepo from './delete'

describe('DELETE /api/github/repo', () => {
afterEach(() => {
fetchMock.reset()
})

it('should create octokit DELETE repo request', async () => {
const { req } = createMocks()
req.json = vi.fn().mockResolvedValue({
full_name: 'kilip/testing',
})

fetchMock.delete(
{
url: 'path:/repos/kilip/testing',
},
204
)

const response = await deleteRepo(req)

expect(response.ok).toBeTruthy()
})

it('should handle octokit response error', async () => {
const { req } = createMocks()
req.json = vi.fn().mockResolvedValue({
full_name: 'kilip/testing',
})

fetchMock.delete(
{
url: 'path:/repos/kilip/testing',
},
() => {
throw new Error('test error')
}
)

const response = await deleteRepo(req)

expect(response.ok).toBeFalsy()
expect(response.status).toBe(500)
})
})
25 changes: 14 additions & 11 deletions src/pkg/github/api/delete.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { createOctokit } from '../octokit';
import { NextRequest, NextResponse } from 'next/server';
import { createOctokit } from '../octokit'
import { NextRequest, NextResponse } from 'next/server'

export default async function deleteRepo(req: NextRequest){
try{
export default async function deleteRepo(req: NextRequest) {
try {
const octokit = await createOctokit(req)
const params = await req.json()

const [owner,name] = params.full_name.split('/')
const [owner, name] = params.full_name.split('/')
const response = await octokit.repos.delete({
owner,
repo: name
})
return NextResponse.json(response, {status: response.status})
}catch(e: any){
return NextResponse.json({error: e}, {
status: e.status,
repo: name,
})
return NextResponse.json({ ...response }, { status: response.status })
} catch (e: any) {
return NextResponse.json(
{ error: e },
{
status: e.status,
}
)
}
}
64 changes: 64 additions & 0 deletions src/pkg/github/api/patch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import fetchMock from 'fetch-mock'
import { createMocks } from 'node-mocks-http'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { patch } from './patch'

describe('PATCH /api/github/repo', () => {
afterEach(() => {
fetchMock.reset()
})

it('should make octokit patch requests', async () => {
const expected = { name: 'testing' }
const payload = {
owner: 'kilip',
repo: 'testing',
archived: true,
}
const { req } = createMocks()
req.json = vi.fn().mockResolvedValue(payload)

fetchMock.patch(
{
url: 'path:/repos/kilip/testing',
body: { archived: true },
},
{
body: expected,
}
)

const response = await patch(req)

const json = await response.json()

expect(json.data).toEqual(expected)
})

it('should handle octokit error response', async () => {
const payload = {
owner: 'kilip',
repo: 'testing',
archived: true,
}
const { req } = createMocks()
req.json = vi.fn().mockResolvedValue(payload)

fetchMock.patch(
{
url: 'path:/repos/kilip/testing',
body: { archived: true },
},
() => {
throw new Error('test error')
}
)

const response = await patch(req)
const json = await response.json()

expect(response.ok).toBeFalsy()
expect(response.status).toBe(500)
expect(json.error.name).toEqual('HttpError')
})
})
23 changes: 13 additions & 10 deletions src/pkg/github/api/patch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { createOctokit } from '../octokit';
import { NextRequest, NextResponse } from 'next/server';
import { GitHubPatchRepoParams } from '../types';
import { createOctokit } from '../octokit'
import { NextRequest, NextResponse } from 'next/server'
import { GitHubPatchRepoParams } from '../types'

export async function patch(req: NextRequest){
try{
export async function patch(req: NextRequest) {
try {
const octokit = await createOctokit(req)
const params: GitHubPatchRepoParams = await req.json()
const response = await octokit.repos.update(params)
return NextResponse.json(response, {status: response.status})
}catch(e: any){
return NextResponse.json({error: e}, {
status: e.status,
})
return NextResponse.json(response, { status: response.status })
} catch (e: any) {
return NextResponse.json(
{ error: e },
{
status: e.status,
}
)
}
}
74 changes: 56 additions & 18 deletions src/pkg/github/api/search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,85 @@
import { createMocks } from 'node-mocks-http'
import { describe, it, expect, vi } from 'vitest'
import { createMocks, createRequest } from 'node-mocks-http'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import search from './search'
import { GitHubSearchParams } from '../types'
import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'
import fetchMock from 'fetch-mock'

describe('search()', ()=>{
describe('GET /api/github/repo', () => {
beforeEach(async () => {
const session = {
profile: {
login: 'kilip',
},
}

const nextAuth: any = await vi.importActual('next-auth/next')
nextAuth.getServerSession = vi.fn().mockResolvedValue(session)
})

it('should returns search', async () => {
const query = {
owner: 'user:kilip'
owner: '',
} as GitHubSearchParams
const { req } = createMocks({
url: '/api/search',
body: query,
nextUrl: {
searchParams: new URLSearchParams({
owner: 'user:kilip'
})
}
owner: '',
}),
},
})
req.json = vi.fn().mockResolvedValue(query)
const resp = {
total_count: 1,
incomplete_results: false,
items: [{hello: 'world'}]
items: [{ hello: 'world' }],
}


fetchMock.mock({
url: 'https://api.github.com/search/repositories',
query: {
q: 'user:kilip'
fetchMock.mock(
{
url: 'path:/search/repositories',
query: {
q: 'user:kilip',
},
},
{
status: 200,
body: resp,
}
}, {
status: 200,
body: resp
})
)

const response = await search(req)
const json = await response.json()

expect(response).toBeInstanceOf(NextResponse)
expect(json.data).toEqual(resp)
fetchMock.reset()
})

it('handles octokit error response', async () => {
const { req } = createMocks({})
req.nextUrl = {
searchParams: new URLSearchParams({ owner: '' }),
}
fetchMock.get(
{
url: 'path:/search/repositories',
query: {
q: 'user:kilip',
},
},
() => {
throw new Error('test error')
}
)

const response = await search(req)
const json = await response.json()

expect(response.ok).toBeFalsy()
expect(response.status).toBe(500)
expect(json.error.name).toEqual('HttpError')
})
})
Loading