Skip to content

Commit

Permalink
lib: add type tests and continue refactor in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaggyTech committed Dec 28, 2023
1 parent 7d98fc3 commit 34ccde4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 9 deletions.
89 changes: 89 additions & 0 deletions packages/lib/src/api/vpic/__tests__/_decodeVin.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, expectTypeOf, test } from 'vitest'

import { decodeVin, type DecodeVinResponse } from '../_decodeVin'

const vin = 'WA1A4AFY2J2008189'
const modelYearString = '2018'
const modelYearNumber = 2018
const modelYear = modelYearNumber

test('Typecheck: decodeVin() - parameters - ', () => {
expectTypeOf<typeof decodeVin>().toBeFunction()
expectTypeOf<typeof decodeVin>().parameters.toMatchTypeOf<
[
vin: string,
options?: boolean | { modelYear?: string | number } | undefined,
doFetch?: boolean | undefined,
]
>()
})

describe('Typecheck: products() - returns correct type of response data - ', () => {
/*****************************
* doFetch = true | undefined (default)
****************************/
test('with vin', async () => {
const result = await decodeVin(vin)
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin and doFetch = true', async () => {
const result = await decodeVin(vin, true)
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin and options = undefined', async () => {
const result = await decodeVin(vin, undefined)
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin and options = {}', async () => {
const result = await decodeVin(vin, {})
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin, options = {}, and doFetch = true', async () => {
const result = await decodeVin(vin, {}, true)
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin and options.modelYear as string', async () => {
const result = await decodeVin(vin, { modelYear: modelYearString })
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin and options.modelYear as number', async () => {
const result = await decodeVin(vin, { modelYear: modelYearNumber })
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})

test('with vin, options.modelYear, and doFetch = true', async () => {
const result = await decodeVin(vin, { modelYear }, true)
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
})
})

describe('Typecheck: decodeVin() - returns string if doFetch = false - ', () => {
/*****************************
* doFetch = false
****************************/
test('with vin and doFetch = false', async () => {
const result = await decodeVin(vin, false)
expectTypeOf(result).toEqualTypeOf<string>()
})

test('with vin, options = undefined, and doFetch = false', async () => {
const result = await decodeVin(vin, undefined, false)
expectTypeOf(result).toEqualTypeOf<string>()
})

test('with vin, options = {}, and doFetch = false', async () => {
const result = await decodeVin(vin, {}, false)
expectTypeOf(result).toEqualTypeOf<string>()
})

test('with vin, options.modelYear, and doFetch = false', async () => {
const result = await decodeVin(vin, { modelYear }, false)
expectTypeOf(result).toEqualTypeOf<string>()
})
})
23 changes: 15 additions & 8 deletions packages/lib/src/api/vpic/__tests__/_decodeVin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const modelYearString = '2018'
const modelYearNumber = 2018
const modelYear = modelYearNumber

// https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?format=json
// https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?modelYear={modelYear}&format=json

const baseUrl = 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin'
const mockUrl = `${baseUrl}/${vin}?format=json`
const mockUrlModelYear = `${baseUrl}/${vin}?modelYear=${modelYear}&format=json`
Expand Down Expand Up @@ -44,33 +47,33 @@ describe('decodeVin()', () => {
test.each<TestEach>([
// vin with no options
{
description: 'vin with no options',
description: 'vin and no options',
args: [vin],
expectedUrl: mockUrl,
},
{
description: 'vin with no options and doFetch = true',
description: 'vin, no options, and doFetch = true',
args: [vin, true],
expectedUrl: mockUrl,
},
// options.modelYear
{
description: 'options.modelYear as string',
description: 'vin and options.modelYear as string',
args: [vin, { modelYear: modelYearString }],
expectedUrl: mockUrlModelYear,
},
{
description: 'options.modelYear as string and doFetch = true',
description: 'vin, options.modelYear as string, and doFetch = true',
args: [vin, { modelYear: modelYearString }, true],
expectedUrl: mockUrlModelYear,
},
{
description: 'options.modelYear as number',
description: 'vin and options.modelYear as number',
args: [vin, { modelYear: modelYearNumber }],
expectedUrl: mockUrlModelYear,
},
{
description: 'options.modelYear as number and doFetch = true',
description: 'vin, options.modelYear as number, and doFetch = true',
args: [vin, { modelYear: modelYearNumber }, true],
expectedUrl: mockUrlModelYear,
},
Expand All @@ -96,12 +99,12 @@ describe('decodeVin()', () => {
},
// options.modelYear
{
description: 'options.modelYear as string and doFetch = false',
description: 'vin, options.modelYear as string and doFetch = false',
args: [vin, { modelYear: modelYearString }, false],
expectedUrl: mockUrlModelYear,
},
{
description: 'options.modelYear as number and doFetch = false',
description: 'vin, options.modelYear as number and doFetch = false',
args: [vin, { modelYear: modelYearNumber }, false],
expectedUrl: mockUrlModelYear,
},
Expand Down Expand Up @@ -338,9 +341,13 @@ describe.skip('IDE Tooltips - manual test of results type on hover', async () =>
/******Expected Tooltip*******\
const result_x: string
******************************/

/* https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?format=json */
const result_1 = await decodeVin(vin, false)
const result_2 = await decodeVin(vin, {}, false)
const result_3 = await decodeVin(vin, undefined, false)

/* https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?modelYear={modelYear}&format=json */
const result_4 = await decodeVin(vin, { modelYear: modelYearString }, false)
const result_5 = await decodeVin(vin, { modelYear: modelYearNumber }, false)

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/api/vpic/_decodeVin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function decodeVin(
): Promise<DecodeVinResponse>
function decodeVin(
vin: string,
options: { modelYear?: string | number },
options: { modelYear?: string | number } | undefined,
doFetch: false
): Promise<string>
function decodeVin(vin: string, doFetch: true): Promise<DecodeVinResponse>
Expand Down

0 comments on commit 34ccde4

Please sign in to comment.