]> git.r.bdr.sh - rbdr/forum/blame - src/lib/utils/glyph_hash.test.ts
Start migration to supabase
[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 = {
cac85db0 5 glyphHash?: GlyphHash;
a7cf03c1 6};
e7f6de3d 7
47b0bfe4 8describe('Glyph Hash utility', () => {
cac85db0
RBR
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 });
e7f6de3d 56});