Skip to content

Commit

Permalink
test: browser tests for new api
Browse files Browse the repository at this point in the history
Closes #485
  • Loading branch information
broofa authored and ctavan committed Jul 22, 2020
1 parent 0e6c10b commit 7fe1381
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ dist/
browserstack.err
local.log
*.tgz
.envrc
26 changes: 24 additions & 2 deletions examples/browser-rollup/example-all.js
@@ -1,9 +1,20 @@
import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid';
import {
parse as uuidParse,
stringify as uuidStringify,
v1 as uuidv1,
v3 as uuidv3,
v4 as uuidv4,
v5 as uuidv5,
validate as uuidValidate,
version as uuidVersion,
} from 'uuid';
import * as uuid from 'uuid';

import testpage from '../utils/testpage';

testpage(function (addTest, done) {
addTest('Named exports');

addTest('uuidv1()', uuidv1());

addTest('uuidv4()', uuidv4());
Expand Down Expand Up @@ -34,7 +45,13 @@ testpage(function (addTest, done) {
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
addTest('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE));

addTest('Same with default export');
// Utility functions
addTest('uuidParse()', uuidParse(MY_NAMESPACE));
addTest('uuidStringify()', uuidStringify(uuidParse(MY_NAMESPACE)));
addTest('uuidValidate()', uuidValidate(MY_NAMESPACE));
addTest('uuidVersion()', uuidVersion(MY_NAMESPACE));

addTest('Default export');

addTest('uuid.v1()', uuid.v1());
addTest('uuid.v4()', uuid.v4());
Expand All @@ -45,5 +62,10 @@ testpage(function (addTest, done) {
addTest('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
addTest('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));

addTest('uuid.parse()', uuid.parse(MY_NAMESPACE));
addTest('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
addTest('uuid.validate()', uuid.validate(MY_NAMESPACE));
addTest('uuid.version()', uuid.version(MY_NAMESPACE));

done();
});
26 changes: 24 additions & 2 deletions examples/browser-webpack/example-all.js
@@ -1,9 +1,20 @@
import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid';
import {
parse as uuidParse,
stringify as uuidStringify,
v1 as uuidv1,
v3 as uuidv3,
v4 as uuidv4,
v5 as uuidv5,
validate as uuidValidate,
version as uuidVersion,
} from 'uuid';
import * as uuid from 'uuid';

import testpage from '../utils/testpage';

testpage(function (addTest, done) {
addTest('Named exports');

addTest('uuidv1()', uuidv1());

addTest('uuidv4()', uuidv4());
Expand Down Expand Up @@ -34,7 +45,13 @@ testpage(function (addTest, done) {
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
addTest('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE));

addTest('Same with default export');
// Utility functions
addTest('uuidParse()', uuidParse(MY_NAMESPACE));
addTest('uuidStringify()', uuidStringify(uuidParse(MY_NAMESPACE)));
addTest('uuidValidate()', uuidValidate(MY_NAMESPACE));
addTest('uuidVersion()', uuidVersion(MY_NAMESPACE));

addTest('Default export');

addTest('uuid.v1()', uuid.v1());
addTest('uuid.v4()', uuid.v4());
Expand All @@ -45,5 +62,10 @@ testpage(function (addTest, done) {
addTest('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
addTest('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));

addTest('uuid.parse()', uuid.parse(MY_NAMESPACE));
addTest('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
addTest('uuid.validate()', uuid.validate(MY_NAMESPACE));
addTest('uuid.version()', uuid.version(MY_NAMESPACE));

done();
});
38 changes: 26 additions & 12 deletions examples/utils/testpage.js
@@ -1,23 +1,37 @@
export default function test(callback) {
const ul = document.createElement('ul');
document.body.appendChild(ul);
const style = document.createElement('style');
style.appendChild(
document.createTextNode(
[
'body {font-family: monospace;}',
'dt, dd {display: inline-block; margin: 0;}',
'dt {min-width: 15em;}',
].join('\n')
)
);
document.body.appendChild(style);

function addTest(title, result) {
const li = document.createElement('li');
// join() result if it's array-like
if (result instanceof Uint8Array || Array.isArray(result)) {
result = Array.prototype.join.apply(result);
}

const h2 = document.createElement('h2');
h2.innerHTML = title;
li.appendChild(h2);
let el;
if (result === undefined) {
el = document.createElement('h2');
el.innerHTML = title;
} else {
el = document.createElement('div');
el.className = 'test_result';
el.innerHTML = '<dt>' + title + '</dt>: <dd>' + result + '</dd>';
}

const p = document.createElement('p');
p.innerHTML = result;
li.appendChild(p);

ul.appendChild(li);
document.body.appendChild(el);
}

function done() {
const div = document.createElement('div');
const div = document.createElement('h2');
div.id = 'done';
document.body.appendChild(div);
}
Expand Down
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prettier.config.js
Expand Up @@ -3,5 +3,5 @@ module.exports = {
printWidth: 100,
proseWrap: 'never',
singleQuote: true,
trailingComma: 'all',
trailingComma: 'es5',
};
2 changes: 1 addition & 1 deletion src/rng-browser.js
Expand Up @@ -17,7 +17,7 @@ const rnds8 = new Uint8Array(16);
export default function rng() {
if (!getRandomValues) {
throw new Error(
'crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported',
'crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported'
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/uuid-bin.js
Expand Up @@ -14,7 +14,7 @@ function usage() {
console.log(' uuid v5 <name> <namespace uuid>');
console.log(' uuid --help');
console.log(
'\nNote: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs defined by RFC4122',
'\nNote: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs defined by RFC4122'
);
}

Expand Down
34 changes: 25 additions & 9 deletions test/browser/browser.spec.js
@@ -1,10 +1,10 @@
/* global browser:false, $:false, $$:false */

const v1Regex = new RegExp(
/^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
/^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
);
const v4Regex = new RegExp(
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
);

const v1 = (result) => expect(result).toMatch(v1Regex);
Expand All @@ -15,7 +15,12 @@ const v3custom = (result) => expect(result).toBe('f5a52d34-dcd7-30f7-b581-0112fa
const v5dns = (result) => expect(result).toBe('fdda765f-fc57-5604-a269-52a7df8164ec');
const v5url = (result) => expect(result).toBe('3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
const v5custom = (result) => expect(result).toBe('c49c5142-4d9a-5940-a926-612ede0ec632');
const ignore = (result) => true;

const parse = (result) =>
expect(result).toEqual('85,35,141,21,201,38,69,152,180,157,207,78,145,59,161,60');
const stringify = (result) => expect(result).toBe('55238d15-c926-4598-b49d-cf4e913ba13c');
const validate = (result) => expect(result).toBe('true');
const version = (result) => expect(result).toBe('4');

const expectations = {
'uuidv1()': v1,
Expand All @@ -26,7 +31,12 @@ const expectations = {
'uuidv5() DNS': v5dns,
'uuidv5() URL': v5url,
'uuidv5() MY_NAMESPACE': v5custom,
'Same with default export': ignore,

'uuidParse()': parse,
'uuidStringify()': stringify,
'uuidValidate()': validate,
'uuidVersion()': version,

'uuid.v1()': v1,
'uuid.v4()': v4,
'uuid.v3() DNS': v3dns,
Expand All @@ -35,6 +45,11 @@ const expectations = {
'uuid.v5() DNS': v5dns,
'uuid.v5() URL': v5url,
'uuid.v5() MY_NAMESPACE': v5custom,

'uuid.parse()': parse,
'uuid.stringify()': stringify,
'uuid.validate()': validate,
'uuid.version()': version,
};
const expectationTitles = Object.keys(expectations);

Expand All @@ -47,7 +62,7 @@ describe('BrowserStack Local Testing', () => {

await browser.waitUntil(async () => $('#done').isExisting(), 30000);

const elements = await $$('li');
const elements = await $$('.test_result');

// Unfortunately the WebDriver API is not thread safe and we cannot use Promise.all() to
// query it in parallel:
Expand All @@ -56,10 +71,11 @@ describe('BrowserStack Local Testing', () => {

for (let i = 0; i < elements.length; ++i) {
const element = elements[i];
const h2 = await element.$('h2');
const title = await h2.getText();
const p = await element.$('p');
const result = await p.getText();
const titleEl = await element.$('dt');
const title = await titleEl.getText();
const resultEl = await element.$('dd');
const result = await resultEl.getText();

expectations[title](result);
titles.push(title);
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit/parse.test.js
Expand Up @@ -34,7 +34,7 @@ describe('parse', () => {
0x3a,
0x08,
0x4b,
]),
])
);
});

Expand All @@ -49,14 +49,14 @@ describe('parse', () => {
// Verify upper/lower case neutrality
assert.deepStrictEqual(
parse('0f5abcd1-c194-47f3-905b-2df7263a084b'),
parse('0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()),
parse('0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase())
);
});

test('Null UUID case', () => {
assert.deepStrictEqual(
parse('00000000-0000-0000-0000-000000000000'),
Uint8Array.from(new Array(16).fill(0)),
Uint8Array.from(new Array(16).fill(0))
);
});

Expand Down
4 changes: 2 additions & 2 deletions test/unit/v1.test.js
Expand Up @@ -53,7 +53,7 @@ describe('v1', () => {
const uidtb = v1({ msecs: TIME - 1 });
assert(
parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
'Clock regression by msec increments the clockseq',
'Clock regression by msec increments the clockseq'
);
});

Expand All @@ -63,7 +63,7 @@ describe('v1', () => {
const uidtnb = v1({ msecs: TIME, nsecs: 9 });
assert(
parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
'Clock regression by nsec increments the clockseq',
'Clock regression by nsec increments the clockseq'
);
});

Expand Down
20 changes: 10 additions & 10 deletions test/unit/v35.test.js
Expand Up @@ -73,29 +73,29 @@ describe('v35', () => {

assert.strictEqual(
v3('http://example.com/hello', v3.URL),
'c6235813-3ba4-3801-ae84-e0a6ebb7d138',
'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
);

assert.strictEqual(
v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'),
'a981a0c2-68b1-35dc-bcfc-296e52ab01ec',
'a981a0c2-68b1-35dc-bcfc-296e52ab01ec'
);
});

test('v3 namespace.toUpperCase', () => {
assert.strictEqual(
v3('hello.example.com', v3.DNS.toUpperCase()),
'9125a8dc-52ee-365b-a5aa-81b0b3681cf6',
'9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
);

assert.strictEqual(
v3('http://example.com/hello', v3.URL.toUpperCase()),
'c6235813-3ba4-3801-ae84-e0a6ebb7d138',
'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
);

assert.strictEqual(
v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()),
'a981a0c2-68b1-35dc-bcfc-296e52ab01ec',
'a981a0c2-68b1-35dc-bcfc-296e52ab01ec'
);
});

Expand Down Expand Up @@ -168,30 +168,30 @@ describe('v35', () => {

assert.strictEqual(
v5('http://example.com/hello', v5.URL),
'3bbcee75-cecc-5b56-8031-b6641c1ed1f1',
'3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
);

assert.strictEqual(
v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'),
'90123e1c-7512-523e-bb28-76fab9f2f73d',
'90123e1c-7512-523e-bb28-76fab9f2f73d'
);
});

test('v5 namespace.toUpperCase', () => {
// Expect to get the same results as http://tools.adjet.org/uuid-v5
assert.strictEqual(
v5('hello.example.com', v5.DNS.toUpperCase()),
'fdda765f-fc57-5604-a269-52a7df8164ec',
'fdda765f-fc57-5604-a269-52a7df8164ec'
);

assert.strictEqual(
v5('http://example.com/hello', v5.URL.toUpperCase()),
'3bbcee75-cecc-5b56-8031-b6641c1ed1f1',
'3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
);

assert.strictEqual(
v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()),
'90123e1c-7512-523e-bb28-76fab9f2f73d',
'90123e1c-7512-523e-bb28-76fab9f2f73d'
);
});

Expand Down

0 comments on commit 7fe1381

Please sign in to comment.