deno.land / x / postal_code@v0.1.0 / postal_code.test.ts

postal_code.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { afterEach, assertEquals, assertFalse, assertNotMatch, assertRejects, assertThrows, describe, fail, it,} from "./dev_deps.ts";import { createPostalCode, PostalCode } from "./postal_code.ts";
Deno.test(`blank or mismatched country code argument will failed`, () => { assertRejects( async () => { await createPostalCode(""); }, Deno.errors.NotSupported, "Not Supported the Options!", );
assertRejects( async () => { await createPostalCode(1); }, Deno.errors.NotSupported, "Not Supported the Options!", );
assertRejects( async () => { await createPostalCode("US"); }, Deno.errors.NotSupported, "Not Supported the Options!", );});
describe(`createPostalCode()`, () => { const testJsonPath = "./.test/th/postal_codes.json"; const testResourceUrl = "https://raw.githubusercontent.com/santa-soft/postal-code/main/.local/th/postal_codes.json"; it(`resource file 'postal_codes.json' initially non-existed`, () => { assertRejects(() => Deno.lstat(testJsonPath), Deno.errors.NotFound); });
describe(`resource file 'postal_codes.json' be loaded when initialize`, () => { afterEach(async () => await cleanup(testJsonPath));
it(`initial with 'th':string is OK`, async () => { const actualPostString = await createPostalCode("th", { resourceUrl: testResourceUrl, cachePath: testJsonPath, }); assertEquals(actualPostString.constructor.name, "PostalCode"); assertEquals(actualPostString.options.country, "th"); assertEquals(actualPostString.options.resourceUrl, testResourceUrl); assertEquals(actualPostString.options.cachePath, testJsonPath); try { const testFileInfo = Deno.lstatSync(testJsonPath); assertFalse(testFileInfo.size <= 0); } catch (_) { // shouldn't come here fail(); } }); it(`initial with 66:number is OK`, async () => { const actualPostNumber = await createPostalCode(66, { cachePath: testJsonPath, }); assertEquals(actualPostNumber.constructor.name, "PostalCode"); assertEquals(actualPostNumber.options.country, "th"); assertEquals(actualPostNumber.options.resourceUrl, testResourceUrl); assertEquals(actualPostNumber.options.cachePath, testJsonPath); try { const testFileInfo = Deno.lstatSync(testJsonPath); assertFalse(testFileInfo.size <= 0); } catch (_) { // shouldn't come here fail(); } }); });});
describe(`PostalCode`, () => { afterEach(async () => await cleanup(testJsonPath));
const testJsonPath = "./.test/th/postal_codes.json";
const postalCode = new PostalCode({ country: "th", cachePath: "./.test/th/test_codes.json", lang: "th", });
it(`addressOf(xxxxx) get address`, () => { const address = postalCode.addressOf(10270); assertFalse(!Array.isArray(address)); assertFalse(address?.length === 0); assertNotMatch(address?.at(0)?.province as string, /[A-Za-z]/); assertNotMatch(address?.at(0)?.district as string, /[A-Za-z]/); assertNotMatch(address?.at(0)?.subDistrict as string, /[A-Za-z]/); });
it(`addressOf() will error when passed string contains non-numeric character`, () => { assertThrows(() => { postalCode.addressOf("101SO"); }, Deno.errors.NotSupported); });});
const cleanup = async (filePath: string) => { try { await Deno.remove(filePath); } catch (_) { console.error("test file deletion error"); }};
postal_code

Version Info

Tagged at
a year ago