Skip to content

Commit

Permalink
test: Refactor tests to run all in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Nov 7, 2019
1 parent 8e41bdc commit 1b075c5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 87 deletions.
16 changes: 16 additions & 0 deletions src/test/error.spec.ts
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));
}
});
1 change: 1 addition & 0 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './error.spec.js';
import './normalize-diacritics.spec.js';
110 changes: 23 additions & 87 deletions src/test/normalize-diacritics.spec.ts
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);
});

0 comments on commit 1b075c5

Please sign in to comment.