aboutsummaryrefslogtreecommitdiff
path: root/src/lib/utils/glyph_hash.test.ts
blob: e791babe4dd12bb13259254535c88dfeff978daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { getGlyphHash } from './glyph_hash';
import type { GlyphHash } from './glyph_hash';

type TestState = {
	glyphHash?: GlyphHash;
};

describe('Glyph Hash utility', () => {
	test('it throws an exception if the string is too short', () => {
		expect(() => {
			getGlyphHash('short');
		}).toThrow();

		expect(() => {
			getGlyphHash('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
		}).toThrow();

		expect(() => {
			getGlyphHash('abcdefghijklmnopqrstuvwxyzABCDEF');
		}).not.toThrow();
	});

	test('it treats UUIDs with hyphens the same as those without', () => {
		const uuidWithHyphens = 'f7722355-2285-46c0-a55f-3483a826f3a6';
		const uuidWithoutHyphens = 'f7722355228546c0a55f3483a826f3a6';

		expect(getGlyphHash(uuidWithHyphens)).toEqual(getGlyphHash(uuidWithoutHyphens));
	});

	describe('it generates four sets of glyphs and colors', () => {
		const state: TestState = {};

		beforeEach(() => {
			state.glyphHash = getGlyphHash('f7722355-2285-46c0-a55f-3483a826f3a6');
		});

		test('there should be four glyph fragments', () => {
			expect(state.glyphHash.length).toBe(4);
		});

		test('each fragment should have a single character glyph', () => {
			for (const glyphHashFragment of state.glyphHash) {
				expect(typeof glyphHashFragment.glyph).toBe('string');
				expect(glyphHashFragment.glyph.length).toBe(1);
			}
		});

		test('each fragment should have a hexadecimal color', () => {
			for (const glyphHashFragment of state.glyphHash) {
				expect(typeof glyphHashFragment.color).toBe('string');
				expect(glyphHashFragment.color.length).toBe(7);
				expect(glyphHashFragment.color).toEqual(expect.stringMatching(/#[0-9a-f]{6}/));
			}
		});
	});
});