-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[META] - Converted
makeId
to TS + tests (#1759)
* [Pagination] - Updated Changelog * Code Review - Moved Changelog entry to current master * [META] - Converted to TS + tests * Updated Changelog * Added Non-null assertion operator before graphicColors * [META] - Converted to TS + tests * Updated Changelog * Code review fixes
- Loading branch information
1 parent
4bdad39
commit 9595574
Showing
4 changed files
with
28 additions
and
3 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
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
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,21 @@ | ||
import makeId from './make_id'; | ||
|
||
describe('makeId', () => { | ||
let ids: Map<string, boolean>; | ||
beforeEach(() => { | ||
ids = new Map<string, boolean>(); | ||
}); | ||
|
||
test('returns a string of length 8', () => { | ||
expect(makeId()).toHaveLength(8); | ||
}); | ||
|
||
// Could be slow so adding a [SLOW] tag for use with --testNamePattern=<regex> | ||
test('returns a random string - [SLOW]', () => { | ||
for (let i = 0; i < 60000; i += 1) { | ||
const id: string = makeId(); | ||
expect(ids.has(id)).toBeFalsy(); | ||
ids.set(id, true); | ||
} | ||
}); | ||
}); |
8 changes: 6 additions & 2 deletions
8
src/components/form/form_row/make_id.js → src/components/form/form_row/make_id.ts
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,5 +1,9 @@ | ||
// Generate statistically almost-certainly-unique `id`s for associating form | ||
// inputs with their labels and other descriptive text elements. | ||
export default function makeId() { | ||
return Math.random().toString(36).slice(-8); | ||
function makeId(): string { | ||
return Math.random() | ||
.toString(36) | ||
.slice(-8); | ||
} | ||
|
||
export default makeId; |