|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import type { TestProviderConfig, TestProviderState } from 'storybook/internal/core-events'; |
| 4 | +import { ManagerContext } from 'storybook/internal/manager-api'; |
| 5 | +import { styled } from 'storybook/internal/theming'; |
| 6 | +import { Addon_TypesEnum } from 'storybook/internal/types'; |
| 7 | + |
| 8 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 9 | +import { fn, within } from '@storybook/test'; |
| 10 | + |
| 11 | +import type { Config, Details } from '../constants'; |
| 12 | +import { TestProviderRender } from './TestProviderRender'; |
| 13 | + |
| 14 | +type Story = StoryObj<typeof TestProviderRender>; |
| 15 | +const managerContext: any = { |
| 16 | + state: { |
| 17 | + testProviders: { |
| 18 | + 'test-provider-id': { |
| 19 | + id: 'test-provider-id', |
| 20 | + name: 'Test Provider', |
| 21 | + type: Addon_TypesEnum.experimental_TEST_PROVIDER, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + api: { |
| 26 | + getDocsUrl: fn().mockName('api::getDocsUrl'), |
| 27 | + emit: fn().mockName('api::emit'), |
| 28 | + updateTestProviderState: fn().mockName('api::updateTestProviderState'), |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +const config: TestProviderConfig = { |
| 33 | + id: 'test-provider-id', |
| 34 | + name: 'Test Provider', |
| 35 | + type: Addon_TypesEnum.experimental_TEST_PROVIDER, |
| 36 | + runnable: true, |
| 37 | + watchable: true, |
| 38 | +}; |
| 39 | + |
| 40 | +const baseState: TestProviderState<Details, Config> = { |
| 41 | + cancellable: true, |
| 42 | + cancelling: false, |
| 43 | + crashed: false, |
| 44 | + error: null, |
| 45 | + failed: false, |
| 46 | + running: false, |
| 47 | + watching: false, |
| 48 | + config: { |
| 49 | + a11y: false, |
| 50 | + coverage: false, |
| 51 | + }, |
| 52 | + details: { |
| 53 | + testResults: [ |
| 54 | + { |
| 55 | + endTime: 0, |
| 56 | + startTime: 0, |
| 57 | + status: 'passed', |
| 58 | + message: 'All tests passed', |
| 59 | + results: [ |
| 60 | + { |
| 61 | + storyId: 'story-id', |
| 62 | + status: 'success', |
| 63 | + duration: 100, |
| 64 | + testRunId: 'test-run-id', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | +}; |
| 71 | + |
| 72 | +const Content = styled.div({ |
| 73 | + padding: '12px 6px', |
| 74 | + display: 'flex', |
| 75 | + flexDirection: 'column', |
| 76 | + gap: '12px', |
| 77 | +}); |
| 78 | + |
| 79 | +export default { |
| 80 | + title: 'TestProviderRender', |
| 81 | + component: TestProviderRender, |
| 82 | + args: { |
| 83 | + state: { |
| 84 | + ...config, |
| 85 | + ...baseState, |
| 86 | + }, |
| 87 | + api: managerContext.api, |
| 88 | + }, |
| 89 | + decorators: [ |
| 90 | + (StoryFn) => ( |
| 91 | + <Content> |
| 92 | + <StoryFn /> |
| 93 | + </Content> |
| 94 | + ), |
| 95 | + (StoryFn) => ( |
| 96 | + <ManagerContext.Provider value={managerContext}> |
| 97 | + <StoryFn /> |
| 98 | + </ManagerContext.Provider> |
| 99 | + ), |
| 100 | + ], |
| 101 | +} as Meta<typeof TestProviderRender>; |
| 102 | + |
| 103 | +export const Default: Story = { |
| 104 | + args: { |
| 105 | + state: { |
| 106 | + ...config, |
| 107 | + ...baseState, |
| 108 | + }, |
| 109 | + }, |
| 110 | +}; |
| 111 | + |
| 112 | +export const Running: Story = { |
| 113 | + args: { |
| 114 | + state: { |
| 115 | + ...config, |
| 116 | + ...baseState, |
| 117 | + running: true, |
| 118 | + }, |
| 119 | + }, |
| 120 | +}; |
| 121 | + |
| 122 | +export const EnableA11y: Story = { |
| 123 | + args: { |
| 124 | + state: { |
| 125 | + ...config, |
| 126 | + ...baseState, |
| 127 | + details: { |
| 128 | + testResults: [], |
| 129 | + }, |
| 130 | + config: { |
| 131 | + a11y: true, |
| 132 | + coverage: false, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | +}; |
| 137 | + |
| 138 | +export const EnableEditing: Story = { |
| 139 | + args: { |
| 140 | + state: { |
| 141 | + ...config, |
| 142 | + ...baseState, |
| 143 | + config: { |
| 144 | + a11y: true, |
| 145 | + coverage: false, |
| 146 | + }, |
| 147 | + details: { |
| 148 | + testResults: [], |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + |
| 153 | + play: async ({ canvasElement }) => { |
| 154 | + const screen = within(canvasElement); |
| 155 | + |
| 156 | + screen.getByLabelText('Edit').click(); |
| 157 | + }, |
| 158 | +}; |
0 commit comments