]> git.r.bdr.sh - rbdr/forum/blob - src/lib/utils/glyph_hash.test.ts
5f57a2a3be2ec3aed673c8bae6e7e0df7a0aa600
[rbdr/forum] / src / lib / utils / glyph_hash.test.ts
1 import { getGlyphHash } from './glyph_hash';
2 import type { GlyphHash } from './glyph_hash';
3
4 type TestState = {
5 glyphHash?: GlyphHash
6 };
7
8
9 describe('Glyph Hash utility', () => {
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
39 const state: TestState = {};
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 });