Skip to content

Commit

Permalink
tests: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostrider-05 committed Apr 10, 2024
1 parent d94f6cd commit 167e620
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 8 deletions.
51 changes: 49 additions & 2 deletions src/__tests__/v2/payloads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,63 @@ import { Type, buildQuery } from '../../v2'
describe('campaign payload', () => {
test('single', () => {
assertType<{
links: { self: string },
links: { self: string }
data: { type: Type.Campaign, id: string, attributes: {} }
}>(buildQuery.campaign()()._payload_type)
})

test('campaign with benefits', () => {
assertType<{
links: { self: string }
data: { type: Type.Campaign, id: string, attributes: {} }
included: (
| { type: Type.Benefit, id: string, attributes: {} }
)[]
}>(buildQuery.campaign(['benefits'])()._payload_type)

assertType<{
links: { self: string }
data: { type: Type.Campaign, id: string, attributes: {} }
included: (
| { type: Type.Benefit, id: string, attributes: {} }
)[]
}>(buildQuery.campaign(['benefits'])({ benefit: [] })._payload_type)

assertType<{
links: { self: string }
data: { type: Type.Campaign, id: string, attributes: {} }
included: (
| { type: Type.Benefit, id: string, attributes: { benefit_type: string | null } }
)[]
}>(buildQuery.campaign(['benefits'])({ benefit: ['benefit_type'] })._payload_type)
})

test('campaign with creator', () => {
assertType<{
links: { self: string },
data: { type: Type.Campaign, id: string, attributes: {} }
}>(buildQuery.campaign(['creator'])()._payload_type)
})

test('campaign with goals', () => {
assertType<{
links: { self: string }
data: { type: Type.Campaign, id: string, attributes: {} }
}>(buildQuery.campaign(['goals'])()._payload_type)
})

test('campaign with tiers', () => {
assertType<{
links: { self: string }
data: { type: Type.Campaign, id: string, attributes: {} }
}>(buildQuery.campaign(['tiers'])()._payload_type)
})
})

describe('campaigns payload', () => {
test('list', () => {
assertType<{
meta: { pagination: { total: number, cursors?: { next: string } } },
meta: { pagination: { total: number, cursors?: { next: string } } }
data: { type: Type.Campaign, id: string, attributes: {} }[]
}>(buildQuery.campaigns()()._payload_type)
})
Expand Down
76 changes: 76 additions & 0 deletions src/__tests__/v2/store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable @typescript-eslint/ban-types */
import { expect, describe, test } from 'vitest'

import { PatreonStore } from '../../v2'

const values = new Map<string, string>()
.set('test', 'value')
.set('key', 'test')

describe('KV store', () => {
let throwError = false

const store = new PatreonStore.KV({
get: (key) => new Promise((resolve) => {
const value = values.get(key)
if (!value && throwError) throw new Error()
return resolve(value ?? null)
}),
put: (key, value) => new Promise(resolve => { values.set(key, value); resolve() }),
}, 'token')

test('unset value', async () => {
expect(await store.get()).toBeUndefined()
throwError = true
expect(await store.get()).toBeUndefined()
})

test('update token', async () => {
const token = {
access_token: 'access',
expires_in: '1200',
expires_in_epoch: '10000',
refresh_token: 'refresh',
token_type: 'bearer',
}

await store.put(token)

expect(await store.get()).toEqual(token)
})
})

describe('Fetch store', () => {
const store = new PatreonStore.Fetch('https://localhost:8000', async (url, options) => {
if (options?.method === 'GET' || !options?.method) {
const token = values.get('fetch_token')

return new Response(token, {
status: token ? 200 : 500,
})
} else if (options.method === 'PUT') {
values.set('fetch_token', options.body!)
return new Response(null, {
status: 201,
})
} else return new Response(null, { status: 400 })
})

test('unset value', async () => {
expect(await store.get()).toBeUndefined()
})

test('update token', async () => {
const token = {
access_token: 'access',
expires_in: '1200',
expires_in_epoch: '10000',
refresh_token: 'refresh',
token_type: 'bearer',
}

await store.put(token)

expect(await store.get()).toEqual(token)
})
})
12 changes: 6 additions & 6 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export default defineConfig({

coverage: {
provider: 'istanbul',
exclude: [
// TODO: add tests for clients
'**/rest/v2/clients/*.ts',
'**/rest/v2/oauth2/client.ts',
],
thresholds: {
// TODO: change to 100
// '100': true,
branches: 60,
functions: 60,
lines: 60,
statements: 60,
'100': true,
}
},
},
Expand Down

0 comments on commit 167e620

Please sign in to comment.