Improve tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent 7db8ab1 · Browse files · 3 files·+53 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+53 added−7 removed
- tests:+53 added−7 removed
Provenance
- Commit
3489e1e- Tree
cbb5c70- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
7db8ab1
3 files changed+53 added−7 removed
modified:test/async-format.test.js+12 added-6 removed
12334556789101112121313141516171819202122231824252627282924303132333435let format = require('../async/format')let random = require('../async/random')
it('generates random string', async () => {it('supports generator', async () => { let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1] async function random (size) { async function customRandom (size) { let bytes = [] for (let i = 0; i < size; i += sequence.length) { bytes = bytes.concat(sequence.slice(0, size - i)) } return bytes } let id = await format(random, 'abcde', 4) expect(id).toEqual('adca') expect(await format(customRandom, 'abcde', 4)).toEqual('adca') expect(await format(customRandom, 'abcde', 14)).toEqual('cbadcbadcbadcc')})
it('respects size', async () => { expect(await format(random, 'abcde', 4)).toHaveLength(4) expect(await format(random, 'abcde', 20)).toHaveLength(20)})
it('is ready for errors', async () => { let error = new Error('test') async function random () { async function brokenRandom () { throw error }
let catched try { await format(random, 'abc', 4) await format(brokenRandom, 'abc', 4) } catch (e) { catched = e } expect(catched).toBe(error)})modified:test/browser.test.js+40 added
8 unmodified lines910111213141516171819202117 unmodified lines39404142434445464748495051525354555657585960616263647 unmodified lines727374757677787980818283848586878889909192938 unmodified lines }}
let nanoid = require('../index.browser')let format = require('../format.browser')let random = require('../random.browser')let async = require('../async/index.browser')let asyncFormat = require('../async/format.browser')let asyncRandom = require('../async/random.browser')let url = require('../url')
function times (size, callback) { let array = []17 unmodified linesit('changes ID length', () => { expect(nanoid(10)).toHaveLength(10)})
it('supports generator', () => { let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1] function customRandom (size) { let bytes = [] for (let i = 0; i < size; i += sequence.length) { bytes = bytes.concat(sequence.slice(0, size - i)) } return bytes } expect(format(customRandom, 'abcde', 4)).toEqual('adca') expect(format(customRandom, 'abcde', 20)).toEqual('dcbadcbadcbadcbadcdc')})
it('respects size', () => { expect(format(random, 'abcde', 4)).toHaveLength(4) expect(format(random, 'abcde', 20)).toHaveLength(20)})
it('generates IDs with Promise', async () => { await Promise.all(times(100, async () => { let id = await async() expect(id).toHaveLength(21)7 unmodified linesit('changes ID length in async', async () => { let id = await async(10) expect(id).toHaveLength(10)})
it('supports async generator', async () => { let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1] async function customRandom (size) { let bytes = [] for (let i = 0; i < size; i += sequence.length) { bytes = bytes.concat(sequence.slice(0, size - i)) } return bytes } expect(await asyncFormat(customRandom, 'abcde', 4)).toEqual('adca') expect(await asyncFormat(customRandom, 'abcde', 14)).toEqual('cbadcbadcbadcc')})
it('respects size for async', async () => { expect(await asyncFormat(asyncRandom, 'abcde', 4)).toHaveLength(4) expect(await asyncFormat(asyncRandom, 'abcde', 20)).toHaveLength(20)})modified:test/format.test.js+1 added-1 removed
9 unmodified lines101112131414151617182 unmodified lines9 unmodified lines } return bytes } expect(format(customRandom, 'abcde', 4)).toEqual('adca') expect(format(customRandom, 'abcde', 20)).toEqual('dcbadcbadcbadcbadcdc') expect(format(customRandom, 'abcde', 18)).toEqual('cbadcbadcbadcbadcc')})
it('respects size', () => { expect(format(random, 'abcde', 4)).toHaveLength(4)2 unmodified lines