]> git.r.bdr.sh - rbdr/forum/blame - src/lib/utils/glyph_hash.test.ts
Update / use typescript
[rbdr/forum] / src / lib / utils / glyph_hash.test.ts
CommitLineData
e7f6de3d 1import { getGlyphHash } from './glyph_hash';
a7cf03c1
RBR
2import type { GlyphHash } from './glyph_hash';
3
4type TestState = {
5 glyphHash?: GlyphHash
6};
e7f6de3d
RBR
7
8
47b0bfe4 9describe('Glyph Hash utility', () => {
e7f6de3d
RBR
10
11 test('it throws an exception if the string is too short', () => {
12
13 expect(() => {
14
15 getGlyphHash('short');
16 }).toThrow();
17
18 expect(() => {
19
20 getGlyphHash('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
21 }).toThrow();
22
23 expect(() => {
24
25 getGlyphHash('abcdefghijklmnopqrstuvwxyzABCDEF');
26 }).not.toThrow();
27 });
28
29 test('it treats UUIDs with hyphens the same as those without', () => {
30
31 const uuidWithHyphens = 'f7722355-2285-46c0-a55f-3483a826f3a6';
32 const uuidWithoutHyphens = 'f7722355228546c0a55f3483a826f3a6';
33
34 expect(getGlyphHash(uuidWithHyphens)).toEqual(getGlyphHash(uuidWithoutHyphens));
35 });
36
37 describe('it generates four sets of glyphs and colors', () => {
38
a7cf03c1 39 const state: TestState = {};
e7f6de3d
RBR
40
41 beforeEach(() => {
42
43 state.glyphHash = getGlyphHash('f7722355-2285-46c0-a55f-3483a826f3a6');
44 });
45
46 test('there should be four glyph fragments', () => {
47
48 expect(state.glyphHash.length).toBe(4);
49 });
50
51 test('each fragment should have a single character glyph', () => {
52
53 for (const glyphHashFragment of state.glyphHash) {
54 expect(typeof glyphHashFragment.glyph).toBe('string');
55 expect(glyphHashFragment.glyph.length).toBe(1);
56 }
57 });
58
59 test('each fragment should have a hexadecimal color', () => {
60
61 for (const glyphHashFragment of state.glyphHash) {
62 expect(typeof glyphHashFragment.color).toBe('string');
63 expect(glyphHashFragment.color.length).toBe(7);
64 expect(glyphHashFragment.color).toEqual(expect.stringMatching(/#[0-9a-f]{6}/));
65 }
66 });
67 });
68});