|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite'; |
| 2 | +import { HttpResponse, http } from 'msw'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { expect, userEvent, waitFor, within } from 'storybook/test'; |
| 5 | + |
| 6 | +import { ReviewContext } from '@/feature/shared/context/review-context'; |
| 7 | +import { StoreSearch } from '@/widgets/landing/ui/store-search'; |
| 8 | + |
| 9 | +const STORE_NAME = '๊ธฐ์ ์ดํ์์๋'; |
| 10 | +const EVENT_NAME = '์ ๊ท ๋ฉ๋ด ๋ถ๋ญ๋ณถ์๋ฉด ๋จน๊ณ ๋ฆฌ๋ทฐ ๋ฌ๊ธฐ'; |
| 11 | + |
| 12 | +const StoreSearchWithReview = () => { |
| 13 | + const [storeId, setStoreId] = useState<string | null>(null); |
| 14 | + const [eventId, setEventId] = useState<string | null>(null); |
| 15 | + return ( |
| 16 | + <ReviewContext.Provider |
| 17 | + value={{ |
| 18 | + storeId, |
| 19 | + eventId, |
| 20 | + setStore: (id) => { |
| 21 | + setStoreId(id); |
| 22 | + setEventId(null); |
| 23 | + }, |
| 24 | + setEvent: setEventId, |
| 25 | + }} |
| 26 | + > |
| 27 | + <StoreSearch /> |
| 28 | + </ReviewContext.Provider> |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +const meta: Meta<typeof StoreSearchWithReview> = { |
| 33 | + title: 'Landing/StoreSearch', |
| 34 | + component: StoreSearchWithReview, |
| 35 | +}; |
| 36 | + |
| 37 | +export default meta; |
| 38 | +type Story = StoryObj<typeof StoreSearchWithReview>; |
| 39 | + |
| 40 | +const selectStore = async ( |
| 41 | + canvas: ReturnType<typeof within>, |
| 42 | + storeName = STORE_NAME |
| 43 | +) => { |
| 44 | + const storeCard = await canvas.findByText(storeName); |
| 45 | + await userEvent.click(storeCard); |
| 46 | +}; |
| 47 | + |
| 48 | +// โโโ ์ฑ๊ณต ์ผ์ด์ค โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| 49 | + |
| 50 | +export const Default: Story = { |
| 51 | + name: '200 ๊ฐ๊ฒ ๋ชฉ๋ก ๋ก๋', |
| 52 | + play: async ({ canvasElement }) => { |
| 53 | + const canvas = within(canvasElement); |
| 54 | + await expect(canvas.findByText(STORE_NAME)).resolves.toBeInTheDocument(); |
| 55 | + await expect( |
| 56 | + canvas.findByText('์ด์ ๊ตฌ ๋ฒ ์ด์ปค๋ฆฌ') |
| 57 | + ).resolves.toBeInTheDocument(); |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +export const SelectStore: Story = { |
| 62 | + name: '200 ๊ฐ๊ฒ ์ ํ ํ ์ด๋ฒคํธ ํ์', |
| 63 | + play: async ({ canvasElement }) => { |
| 64 | + const canvas = within(canvasElement); |
| 65 | + await selectStore(canvas); |
| 66 | + await expect(canvas.findByText(EVENT_NAME)).resolves.toBeInTheDocument(); |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +export const SelectStoreAndEvent: Story = { |
| 71 | + name: '200 ๊ฐ๊ฒยท์ด๋ฒคํธ ์ ํ ํ ๋ค์ ๋จ๊ณ ํ์ฑํ', |
| 72 | + play: async ({ canvasElement }) => { |
| 73 | + const canvas = within(canvasElement); |
| 74 | + await selectStore(canvas); |
| 75 | + const eventCard = await canvas.findByText(EVENT_NAME); |
| 76 | + await userEvent.click(eventCard); |
| 77 | + const button = canvas.getByRole('button', { name: '๋ค์ ๋จ๊ณ' }); |
| 78 | + expect(button).not.toBeDisabled(); |
| 79 | + }, |
| 80 | +}; |
| 81 | + |
| 82 | +export const SearchFilter: Story = { |
| 83 | + name: '200 ๊ฒ์์ด๋ก ๊ฐ๊ฒ ํํฐ๋ง', |
| 84 | + play: async ({ canvasElement }) => { |
| 85 | + const canvas = within(canvasElement); |
| 86 | + await expect( |
| 87 | + canvas.findByText('์ด์ ๊ตฌ ๋ฒ ์ด์ปค๋ฆฌ') |
| 88 | + ).resolves.toBeInTheDocument(); |
| 89 | + const searchInput = canvas.getByPlaceholderText('๊ฐ๊ฒ ์ด๋ฆ์ ๊ฒ์ํ์ธ์'); |
| 90 | + await userEvent.type(searchInput, '์๋'); |
| 91 | + // 1๋จ๊ณ: ๋๋ฐ์ด์ค(300ms) ํ ์ด์ ๋ชฉ๋ก์ด ์ฌ๋ผ์ง ๋๊น์ง ๋๊ธฐ |
| 92 | + await waitFor( |
| 93 | + () => |
| 94 | + expect(canvas.queryByText('์ด์ ๊ตฌ ๋ฒ ์ด์ปค๋ฆฌ')).not.toBeInTheDocument(), |
| 95 | + { timeout: 2000 } |
| 96 | + ); |
| 97 | + // 2๋จ๊ณ: ์ฟผ๋ฆฌ ์๋ฃ ํ ํํฐ๋ง ๊ฒฐ๊ณผ๊ฐ ๋ํ๋ ๋๊น์ง ๋๊ธฐ (์ด ์์ ์ "๊ฒ์ ์ค..." ์ํ์ผ ์ ์์) |
| 98 | + await waitFor( |
| 99 | + () => expect(canvas.getByText(STORE_NAME)).toBeInTheDocument(), |
| 100 | + { timeout: 2000 } |
| 101 | + ); |
| 102 | + expect(canvas.queryByText('์ด์ ๊ตฌ ๋ฒ ์ด์ปค๋ฆฌ')).not.toBeInTheDocument(); |
| 103 | + }, |
| 104 | +}; |
| 105 | + |
| 106 | +export const NoSearchResults: Story = { |
| 107 | + name: '๊ฒ์ ๊ฒฐ๊ณผ ์์', |
| 108 | + play: async ({ canvasElement }) => { |
| 109 | + const canvas = within(canvasElement); |
| 110 | + await canvas.findByText(STORE_NAME); |
| 111 | + const searchInput = canvas.getByPlaceholderText('๊ฐ๊ฒ ์ด๋ฆ์ ๊ฒ์ํ์ธ์'); |
| 112 | + await userEvent.type(searchInput, '์๋๊ฐ๊ฒ์ด๋ฆ'); |
| 113 | + await expect( |
| 114 | + canvas.findByText('๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.') |
| 115 | + ).resolves.toBeInTheDocument(); |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
| 119 | +// โโโ ์๋ฌ ์ผ์ด์ค โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| 120 | + |
| 121 | +export const ErrorGetStoresFailed: Story = { |
| 122 | + name: '500 ๊ฐ๊ฒ ๋ชฉ๋ก ์กฐํ ์คํจ', |
| 123 | + parameters: { |
| 124 | + msw: { |
| 125 | + handlers: [ |
| 126 | + http.get('*/api/store', () => |
| 127 | + HttpResponse.json( |
| 128 | + { code: 'SERVER_ERROR', message: 'Internal server error' }, |
| 129 | + { status: 500 } |
| 130 | + ) |
| 131 | + ), |
| 132 | + ], |
| 133 | + }, |
| 134 | + }, |
| 135 | + play: async ({ canvasElement }) => { |
| 136 | + const canvas = within(canvasElement); |
| 137 | + await expect( |
| 138 | + canvas.findByText('๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.') |
| 139 | + ).resolves.toBeInTheDocument(); |
| 140 | + }, |
| 141 | +}; |
| 142 | + |
| 143 | +export const ErrorGetStoreEventsFailed: Story = { |
| 144 | + name: '404 ์ด๋ฒคํธ ๋ชฉ๋ก ์กฐํ ์คํจ', |
| 145 | + parameters: { |
| 146 | + msw: { |
| 147 | + handlers: [ |
| 148 | + // events: [] ๋ก ๋ฐํํด fallback๋ ๋น์ด์๋๋ก ์ค์ |
| 149 | + http.get('*/api/store', () => |
| 150 | + HttpResponse.json({ |
| 151 | + stores: [ |
| 152 | + { |
| 153 | + id: 'store-001', |
| 154 | + name: STORE_NAME, |
| 155 | + address: '์์ธํน๋ณ์ ๊ด์
๊ตฌ ๋ด์ฒ๋ 1620-38', |
| 156 | + category: 'RESTAURANT', |
| 157 | + description: '์ด ์๋๋ฅผ ๋จน์ผ๋ฉด ๊ธฐ์ ํด์.', |
| 158 | + events: [], |
| 159 | + totalEventCount: 0, |
| 160 | + }, |
| 161 | + ], |
| 162 | + totalCount: 1, |
| 163 | + currentPage: 0, |
| 164 | + totalPages: 1, |
| 165 | + hasNext: false, |
| 166 | + }) |
| 167 | + ), |
| 168 | + http.get('*/api/store/:storeId/events', () => |
| 169 | + HttpResponse.json( |
| 170 | + { code: 'STORE_001', message: 'Store not found' }, |
| 171 | + { status: 404 } |
| 172 | + ) |
| 173 | + ), |
| 174 | + ], |
| 175 | + }, |
| 176 | + }, |
| 177 | + play: async ({ canvasElement }) => { |
| 178 | + const canvas = within(canvasElement); |
| 179 | + await selectStore(canvas); |
| 180 | + await expect( |
| 181 | + canvas.findByText('๋ฑ๋ก๋ ์ด๋ฒคํธ๊ฐ ์์ต๋๋ค.') |
| 182 | + ).resolves.toBeInTheDocument(); |
| 183 | + }, |
| 184 | +}; |
0 commit comments