summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/MkClickerGame.stories.impl.ts
blob: 36313f965daa944116f14eef02b05a83204aee16 (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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable import/no-default-export */
import { StoryObj } from '@storybook/vue3';
import { HttpResponse, http } from 'msw';
import { action } from '@storybook/addon-actions';
import { expect, userEvent, within } from '@storybook/test';
import { commonHandlers } from '../../.storybook/mocks.js';
import MkClickerGame from './MkClickerGame.vue';

function sleep(ms: number) {
	return new Promise(resolve => setTimeout(resolve, ms));
}

export const Default = {
	render(args) {
		return {
			components: {
				MkClickerGame,
			},
			setup() {
				return {
					args,
				};
			},
			computed: {
				props() {
					return {
						...this.args,
					};
				},
			},
			template: '<MkClickerGame v-bind="props" />',
		};
	},
	async play({ canvasElement }) {
		await sleep(1000);
		const canvas = within(canvasElement);
		const count = canvas.getByTestId('count');
		await expect(count).toHaveTextContent('0');
		const buttonElement = canvas.getByRole<HTMLButtonElement>('button');
		await userEvent.click(buttonElement);
		await expect(count).toHaveTextContent('1');
	},
	parameters: {
		layout: 'centered',
		msw: {
			handlers: [
				...commonHandlers,
				http.post('/api/i/registry/get', async ({ request }) => {
					action('POST /api/i/registry/get')(await request.json());
					return HttpResponse.json({
						error: {
							message: 'No such key.',
							code: 'NO_SUCH_KEY',
							id: 'ac3ed68a-62f0-422b-a7bc-d5e09e8f6a6a',
						},
					}, {
						status: 400,
					});
				}),
				http.post('/api/i/registry/set', async ({ request }) => {
					action('POST /api/i/registry/set')(await request.json());
					return HttpResponse.json(undefined, { status: 204 });
				}),
				http.post('/api/i/claim-achievement', async ({ request }) => {
					action('POST /api/i/claim-achievement')(await request.json());
					return HttpResponse.json(undefined, { status: 204 });
				}),
			],
		},
	},
} satisfies StoryObj<typeof MkClickerGame>;