|
1 | 1 | import type { Meta, StoryObj } from '@storybook/vue3-vite' |
| 2 | +import type { RpcFunctionInfo } from '../connect' |
| 3 | +import { expect, fn, userEvent, within } from 'storybook/test' |
2 | 4 | import FunctionsView from './FunctionsView.vue' |
3 | 5 |
|
4 | 6 | const meta = { |
@@ -119,3 +121,71 @@ export const WithResultsAndPending: Story = { |
119 | 121 | }, |
120 | 122 | }, |
121 | 123 | } |
| 124 | + |
| 125 | +const query: RpcFunctionInfo = { |
| 126 | + name: 'audit:echo', |
| 127 | + type: 'query', |
| 128 | + jsonSerializable: true, |
| 129 | + snapshot: false, |
| 130 | + cacheable: false, |
| 131 | + hasArgs: true, |
| 132 | + hasReturns: false, |
| 133 | + hasDump: false, |
| 134 | + hasSetup: true, |
| 135 | + hasHandler: true, |
| 136 | + invokable: true, |
| 137 | +} |
| 138 | + |
| 139 | +export const InvalidArguments: Story = { |
| 140 | + args: { |
| 141 | + ...Default.args, |
| 142 | + functions: [query], |
| 143 | + onInvoke: fn(), |
| 144 | + }, |
| 145 | + play: async ({ args, canvasElement }) => { |
| 146 | + const canvas = within(canvasElement) |
| 147 | + await userEvent.click(canvas.getByText('echo')) |
| 148 | + const input = canvas.getByPlaceholderText('[]') |
| 149 | + const invoke = canvas.getByRole('button', { name: 'Invoke' }) |
| 150 | + |
| 151 | + await userEvent.type(input, '{{') |
| 152 | + await userEvent.click(invoke) |
| 153 | + const error = canvas.getByRole('alert') |
| 154 | + await expect(error).toHaveTextContent('Invalid JSON arguments:') |
| 155 | + await expect(input).toHaveAttribute('aria-invalid', 'true') |
| 156 | + await expect(input).toHaveAttribute('aria-describedby', error.id) |
| 157 | + await expect(args.onInvoke).not.toHaveBeenCalled() |
| 158 | + |
| 159 | + await userEvent.clear(input) |
| 160 | + await expect(canvas.queryByRole('alert')).not.toBeInTheDocument() |
| 161 | + await expect(input).toHaveAttribute('aria-invalid', 'false') |
| 162 | + await userEvent.type(input, '[["recovered"]') |
| 163 | + await userEvent.click(invoke) |
| 164 | + await expect(args.onInvoke).toHaveBeenLastCalledWith(query, ['recovered']) |
| 165 | + |
| 166 | + await userEvent.clear(input) |
| 167 | + await userEvent.type(input, '42') |
| 168 | + await userEvent.click(invoke) |
| 169 | + await expect(args.onInvoke).toHaveBeenLastCalledWith(query, [42]) |
| 170 | + }, |
| 171 | +} |
| 172 | + |
| 173 | +export const InvalidArgumentsAfterSuccess: Story = { |
| 174 | + args: { |
| 175 | + ...InvalidArguments.args, |
| 176 | + results: { |
| 177 | + 'audit:echo': { ok: true, result: 'previous result', durationMs: 1 }, |
| 178 | + }, |
| 179 | + onInvoke: fn(), |
| 180 | + }, |
| 181 | + play: async ({ args, canvasElement }) => { |
| 182 | + const canvas = within(canvasElement) |
| 183 | + await userEvent.click(canvas.getByText('echo')) |
| 184 | + const input = canvas.getByPlaceholderText('[]') |
| 185 | + await userEvent.type(input, '{{') |
| 186 | + await userEvent.click(canvas.getByRole('button', { name: 'Invoke' })) |
| 187 | + await expect(canvas.getByRole('alert')).toHaveTextContent('Invalid JSON arguments:') |
| 188 | + await expect(canvas.getByText('"previous result"')).toBeVisible() |
| 189 | + await expect(args.onInvoke).not.toHaveBeenCalled() |
| 190 | + }, |
| 191 | +} |
0 commit comments