-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Refactor tests to run all in parallel
- Loading branch information
Showing
3 changed files
with
40 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { normalize } from '../normalize-diacritics.js'; | ||
|
||
const formatErrorMessage = | ||
(s: unknown) => new TypeError(`Expected 'input' to be of type string, but received '${s}'`); | ||
|
||
type TestError = [string, undefined | null | string]; | ||
test.each<TestError>([ | ||
[`null`, null], | ||
[`void`, void 0], | ||
])(`normalize(%s)`, async (_, a) => { | ||
try { | ||
await normalize(a); | ||
} catch (e) { | ||
expect(e).toStrictEqual(formatErrorMessage(a)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './error.spec.js'; | ||
import './normalize-diacritics.spec.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,26 @@ | ||
import { normalize } from '../normalize-diacritics.js'; | ||
|
||
describe('normalize-diacritics', () => { | ||
describe('error', () => { | ||
it(`throws when invalid input`, async () => { | ||
try { | ||
await normalize(null); | ||
} catch (e) { | ||
expect(e).toStrictEqual( | ||
new TypeError(`Expected 'input' to be of type string, but received 'null'`)); | ||
} | ||
}); | ||
|
||
it(`throws when 'input' is 'undefined'`, async () => { | ||
try { | ||
await normalize(); | ||
} catch (e) { | ||
expect(e).toStrictEqual( | ||
new TypeError(`Expected 'input' to be of type string, but received 'undefined'`)); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
describe('ok', () => { | ||
it(`skips normalization for empty character`, async () => { | ||
expect(await normalize('')).toStrictEqual(''); | ||
}); | ||
|
||
it('normalizes accented characters', async () => { | ||
try { | ||
const strs = [ | ||
'Åland Islands', | ||
'Saint Barthélemy', | ||
'Cocos (Keeling) Islands', | ||
`Côte d'Ivoire`, | ||
'Curaçao', | ||
'Réunion', | ||
]; | ||
|
||
expect(await normalize(strs[0])).toStrictEqual('Aland Islands'); | ||
expect(await normalize(strs[1])).toStrictEqual('Saint Barthelemy'); | ||
expect(await normalize(strs[2])).toStrictEqual('Cocos (Keeling) Islands'); | ||
expect(await normalize(strs[3])).toStrictEqual(`Cote d'Ivoire`); | ||
expect(await normalize(strs[4])).toStrictEqual('Curacao'); | ||
expect(await normalize(strs[5])).toStrictEqual('Reunion'); | ||
} catch (e) { | ||
throw e; | ||
} | ||
}); | ||
|
||
it('normalizes accented characters without using native function', async () => { | ||
const cachedFn = String.prototype.normalize; | ||
String.prototype.normalize = null!; | ||
|
||
try { | ||
expect(await normalize('Réunion')).toStrictEqual('Reunion'); | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
String.prototype.normalize = cachedFn; | ||
} | ||
}); | ||
|
||
it(`normalizes non-accented characters (Latin-1 Supplement)`, async () => { | ||
expect(await normalize('tromsø')).toStrictEqual('tromso'); | ||
expect(await normalize('\u00d8')).toStrictEqual('O'); | ||
}); | ||
|
||
it(`normalizes repeated characters`, async () => { | ||
const s = [ | ||
'éééé', | ||
'åååå', | ||
'éåéåéåéå', | ||
'åéåéåéåé', | ||
]; | ||
const d = await Promise.all(s.map(async n => normalize(n))); | ||
const expected = [ | ||
'eeee', | ||
'aaaa', | ||
'eaeaeaea', | ||
'aeaeaeae', | ||
]; | ||
|
||
expect(d).toEqual(expected); | ||
}); | ||
|
||
}); | ||
|
||
type TestSuccess = [string, undefined | null | string, string]; | ||
test.each<TestSuccess>([ | ||
[`''`, '', ''], | ||
[`'Åland Islands'`, 'Åland Islands', 'Aland Islands'], | ||
[`'Saint Barthélemy'`, 'Saint Barthélemy', 'Saint Barthelemy'], | ||
[`'Cocos (Keeling) Islands'`, 'Cocos (Keeling) Islands', 'Cocos (Keeling) Islands'], | ||
[`'Côte d'Ivoire'`, `Côte d'Ivoire`, `Cote d'Ivoire`], | ||
[`'Curaçao'`, `Curaçao`, 'Curacao'], | ||
[`'Réunion'`, `Réunion`, 'Reunion'], | ||
|
||
// non-accented characters (Latin-1 Supplement) | ||
[`'tromsø'`, `tromsø`, 'tromso'], | ||
[`'\u00d8'`, `\u00d8`, 'O'], | ||
|
||
// repeated characters | ||
[`'éééé'`, `éééé`, 'eeee'], | ||
[`'åååå'`, `åååå`, 'aaaa'], | ||
[`'éåéåéåéå'`, `éåéåéåéå`, 'eaeaeaea'], | ||
[`'åéåéåéåé'`, `åéåéåéåé`, 'aeaeaeae'], | ||
])(`normalize(%s)`, async (_, a, expected) => { | ||
const d = await normalize(a); | ||
|
||
expect(d).toStrictEqual(expected); | ||
}); |