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