Skip to content

Commit 34ccde4

Browse files
committed
lib: add type tests and continue refactor in progress
1 parent 7d98fc3 commit 34ccde4

3 files changed

Lines changed: 105 additions & 9 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { describe, expectTypeOf, test } from 'vitest'
2+
3+
import { decodeVin, type DecodeVinResponse } from '../_decodeVin'
4+
5+
const vin = 'WA1A4AFY2J2008189'
6+
const modelYearString = '2018'
7+
const modelYearNumber = 2018
8+
const modelYear = modelYearNumber
9+
10+
test('Typecheck: decodeVin() - parameters - ', () => {
11+
expectTypeOf<typeof decodeVin>().toBeFunction()
12+
expectTypeOf<typeof decodeVin>().parameters.toMatchTypeOf<
13+
[
14+
vin: string,
15+
options?: boolean | { modelYear?: string | number } | undefined,
16+
doFetch?: boolean | undefined,
17+
]
18+
>()
19+
})
20+
21+
describe('Typecheck: products() - returns correct type of response data - ', () => {
22+
/*****************************
23+
* doFetch = true | undefined (default)
24+
****************************/
25+
test('with vin', async () => {
26+
const result = await decodeVin(vin)
27+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
28+
})
29+
30+
test('with vin and doFetch = true', async () => {
31+
const result = await decodeVin(vin, true)
32+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
33+
})
34+
35+
test('with vin and options = undefined', async () => {
36+
const result = await decodeVin(vin, undefined)
37+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
38+
})
39+
40+
test('with vin and options = {}', async () => {
41+
const result = await decodeVin(vin, {})
42+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
43+
})
44+
45+
test('with vin, options = {}, and doFetch = true', async () => {
46+
const result = await decodeVin(vin, {}, true)
47+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
48+
})
49+
50+
test('with vin and options.modelYear as string', async () => {
51+
const result = await decodeVin(vin, { modelYear: modelYearString })
52+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
53+
})
54+
55+
test('with vin and options.modelYear as number', async () => {
56+
const result = await decodeVin(vin, { modelYear: modelYearNumber })
57+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
58+
})
59+
60+
test('with vin, options.modelYear, and doFetch = true', async () => {
61+
const result = await decodeVin(vin, { modelYear }, true)
62+
expectTypeOf(result).toEqualTypeOf<DecodeVinResponse>()
63+
})
64+
})
65+
66+
describe('Typecheck: decodeVin() - returns string if doFetch = false - ', () => {
67+
/*****************************
68+
* doFetch = false
69+
****************************/
70+
test('with vin and doFetch = false', async () => {
71+
const result = await decodeVin(vin, false)
72+
expectTypeOf(result).toEqualTypeOf<string>()
73+
})
74+
75+
test('with vin, options = undefined, and doFetch = false', async () => {
76+
const result = await decodeVin(vin, undefined, false)
77+
expectTypeOf(result).toEqualTypeOf<string>()
78+
})
79+
80+
test('with vin, options = {}, and doFetch = false', async () => {
81+
const result = await decodeVin(vin, {}, false)
82+
expectTypeOf(result).toEqualTypeOf<string>()
83+
})
84+
85+
test('with vin, options.modelYear, and doFetch = false', async () => {
86+
const result = await decodeVin(vin, { modelYear }, false)
87+
expectTypeOf(result).toEqualTypeOf<string>()
88+
})
89+
})

packages/lib/src/api/vpic/__tests__/_decodeVin.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const modelYearString = '2018'
1010
const modelYearNumber = 2018
1111
const modelYear = modelYearNumber
1212

13+
// https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?format=json
14+
// https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?modelYear={modelYear}&format=json
15+
1316
const baseUrl = 'https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin'
1417
const mockUrl = `${baseUrl}/${vin}?format=json`
1518
const mockUrlModelYear = `${baseUrl}/${vin}?modelYear=${modelYear}&format=json`
@@ -44,33 +47,33 @@ describe('decodeVin()', () => {
4447
test.each<TestEach>([
4548
// vin with no options
4649
{
47-
description: 'vin with no options',
50+
description: 'vin and no options',
4851
args: [vin],
4952
expectedUrl: mockUrl,
5053
},
5154
{
52-
description: 'vin with no options and doFetch = true',
55+
description: 'vin, no options, and doFetch = true',
5356
args: [vin, true],
5457
expectedUrl: mockUrl,
5558
},
5659
// options.modelYear
5760
{
58-
description: 'options.modelYear as string',
61+
description: 'vin and options.modelYear as string',
5962
args: [vin, { modelYear: modelYearString }],
6063
expectedUrl: mockUrlModelYear,
6164
},
6265
{
63-
description: 'options.modelYear as string and doFetch = true',
66+
description: 'vin, options.modelYear as string, and doFetch = true',
6467
args: [vin, { modelYear: modelYearString }, true],
6568
expectedUrl: mockUrlModelYear,
6669
},
6770
{
68-
description: 'options.modelYear as number',
71+
description: 'vin and options.modelYear as number',
6972
args: [vin, { modelYear: modelYearNumber }],
7073
expectedUrl: mockUrlModelYear,
7174
},
7275
{
73-
description: 'options.modelYear as number and doFetch = true',
76+
description: 'vin, options.modelYear as number, and doFetch = true',
7477
args: [vin, { modelYear: modelYearNumber }, true],
7578
expectedUrl: mockUrlModelYear,
7679
},
@@ -96,12 +99,12 @@ describe('decodeVin()', () => {
9699
},
97100
// options.modelYear
98101
{
99-
description: 'options.modelYear as string and doFetch = false',
102+
description: 'vin, options.modelYear as string and doFetch = false',
100103
args: [vin, { modelYear: modelYearString }, false],
101104
expectedUrl: mockUrlModelYear,
102105
},
103106
{
104-
description: 'options.modelYear as number and doFetch = false',
107+
description: 'vin, options.modelYear as number and doFetch = false',
105108
args: [vin, { modelYear: modelYearNumber }, false],
106109
expectedUrl: mockUrlModelYear,
107110
},
@@ -338,9 +341,13 @@ describe.skip('IDE Tooltips - manual test of results type on hover', async () =>
338341
/******Expected Tooltip*******\
339342
const result_x: string
340343
******************************/
344+
345+
/* https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?format=json */
341346
const result_1 = await decodeVin(vin, false)
342347
const result_2 = await decodeVin(vin, {}, false)
343348
const result_3 = await decodeVin(vin, undefined, false)
349+
350+
/* https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVin/{vin}?modelYear={modelYear}&format=json */
344351
const result_4 = await decodeVin(vin, { modelYear: modelYearString }, false)
345352
const result_5 = await decodeVin(vin, { modelYear: modelYearNumber }, false)
346353

packages/lib/src/api/vpic/_decodeVin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function decodeVin(
155155
): Promise<DecodeVinResponse>
156156
function decodeVin(
157157
vin: string,
158-
options: { modelYear?: string | number },
158+
options: { modelYear?: string | number } | undefined,
159159
doFetch: false
160160
): Promise<string>
161161
function decodeVin(vin: string, doFetch: true): Promise<DecodeVinResponse>

0 commit comments

Comments
 (0)