summaryrefslogtreecommitdiff
path: root/packages/backend/test/unit/misc/loader.ts
blob: 2cf54e15559a3dcdafbaa58b9f68378fbc030ea7 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { DebounceLoader } from '@/misc/loader.js';

class Mock {
	loadCountByKey = new Map<number, number>();
	load = async (key: number): Promise<number> => {
		const count = this.loadCountByKey.get(key);
		if (typeof count === 'undefined') {
			this.loadCountByKey.set(key, 1);
		} else {
			this.loadCountByKey.set(key, count + 1);
		}
		return key * 2;
	};
	reset() {
		this.loadCountByKey.clear();
	}
}

describe(DebounceLoader, () => {
	describe('single request', () => {
		it('loads once', async () => {
			const mock = new Mock();
			const loader = new DebounceLoader(mock.load);
			expect(await loader.load(7)).toBe(14);
			expect(mock.loadCountByKey.size).toBe(1);
			expect(mock.loadCountByKey.get(7)).toBe(1);
		});
	});

	describe('two duplicated requests at same time', () => {
		it('loads once', async () => {
			const mock = new Mock();
			const loader = new DebounceLoader(mock.load);
			const [v1, v2] = await Promise.all([
				loader.load(7),
				loader.load(7),
			]);
			expect(v1).toBe(14);
			expect(v2).toBe(14);
			expect(mock.loadCountByKey.size).toBe(1);
			expect(mock.loadCountByKey.get(7)).toBe(1);
		});
	});

	describe('two different requests at same time', () => {
		it('loads twice', async () => {
			const mock = new Mock();
			const loader = new DebounceLoader(mock.load);
			const [v1, v2] = await Promise.all([
				loader.load(7),
				loader.load(13),
			]);
			expect(v1).toBe(14);
			expect(v2).toBe(26);
			expect(mock.loadCountByKey.size).toBe(2);
			expect(mock.loadCountByKey.get(7)).toBe(1);
			expect(mock.loadCountByKey.get(13)).toBe(1);
		});
	});

	describe('non-continuous same two requests', () => {
		it('loads twice', async () => {
			const mock = new Mock();
			const loader = new DebounceLoader(mock.load);
			expect(await loader.load(7)).toBe(14);
			expect(mock.loadCountByKey.size).toBe(1);
			expect(mock.loadCountByKey.get(7)).toBe(1);
			mock.reset();
			expect(await loader.load(7)).toBe(14);
			expect(mock.loadCountByKey.size).toBe(1);
			expect(mock.loadCountByKey.get(7)).toBe(1);
		});
	});

	describe('non-continuous different two requests', () => {
		it('loads twice', async () => {
			const mock = new Mock();
			const loader = new DebounceLoader(mock.load);
			expect(await loader.load(7)).toBe(14);
			expect(mock.loadCountByKey.size).toBe(1);
			expect(mock.loadCountByKey.get(7)).toBe(1);
			mock.reset();
			expect(await loader.load(13)).toBe(26);
			expect(mock.loadCountByKey.size).toBe(1);
			expect(mock.loadCountByKey.get(13)).toBe(1);
		});
	});
});