Skip to content

Commit

Permalink
Clean up more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Aug 9, 2024
1 parent df27757 commit 5706cb8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 67 deletions.
22 changes: 10 additions & 12 deletions app/note/[id]/page.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/**
* @vitest-environment jsdom
*/

import { type Meta, type StoryObj } from '@storybook/react'
import { cookies } from '@storybook/nextjs/headers.mock'
import { http } from 'msw'
import { expect, userEvent, waitFor, within } from '@storybook/test'
import { expect, userEvent } from '@storybook/test'
import Page from './page'
import { db, initializeDB } from '#lib/db.mock'
import { createUserCookie, userCookieKey } from '#lib/session'
import { PageDecorator } from '#.storybook/decorators'
import { login } from '#app/actions.mock'
import * as auth from '#app/auth/route'
import { expectRedirect } from '#lib/test-utils'
import NoteSkeleton from '#app/note/[id]/loading'

const meta = {
component: Page,
Expand Down Expand Up @@ -77,12 +74,10 @@ export const LoginShouldGetOAuthTokenAndSetCookie: Story = {
],
},
},
beforeEach() {},
play: async ({ mount }) => {
// Point the login implementation to the endpoint github would have redirected too.
login.mockImplementation(async () => {
const absoluteUrl = new URL('/auth?code=storybookjs', 'http://localhost')
return await auth.GET(new Request(absoluteUrl.toString()))
return await auth.GET(new Request('/auth?code=storybookjs'))
})
const canvas = await mount()
await expect(cookies().get(userCookieKey)?.value).toBeUndefined()
Expand All @@ -95,11 +90,10 @@ export const LoginShouldGetOAuthTokenAndSetCookie: Story = {
}

export const LogoutShouldDeleteCookie: Story = {
async beforeEach() {
async beforeEach() {},
play: async ({ mount }) => {
cookies().set(userCookieKey, await createUserCookie('storybookjs'))
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement)
const canvas = await mount()
await expect(cookies().get(userCookieKey)?.value).toContain('storybookjs')
await userEvent.click(await canvas.findByRole('button', { name: 'logout' }))
await expectRedirect('/')
Expand All @@ -122,3 +116,7 @@ export const EmptyState: Story = {
initializeDB({}) // init an empty DB
},
}

export const Loading: Story = {
render: () => <NoteSkeleton />,
}
17 changes: 5 additions & 12 deletions app/note/edit/[id]/page.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* @vitest-environment jsdom
*/

import { expect, userEvent, waitFor, within } from '@storybook/test'
import { expect, userEvent } from '@storybook/test'
import { type Meta, type StoryObj } from '@storybook/react'
import { cookies } from '@storybook/nextjs/headers.mock'
import Page from './page'
Expand Down Expand Up @@ -54,8 +50,7 @@ export const UnknownId: Story = {
}

export const SavingExistingNoteShouldUpdateDBAndRedirect: Story = {
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
play: async ({ canvasElement, canvas }) => {
const titleInput = await canvas.findByLabelText(
'Enter a title for your note',
)
Expand All @@ -73,7 +68,7 @@ export const SavingExistingNoteShouldUpdateDBAndRedirect: Story = {
await userEvent.click(
await canvas.findByRole('menuitem', { name: /done/i }),
// WORKAROUND: FALSE_POSITIVE_POINTER_EVENTS
{ pointerEventsCheck: 0 }
{ pointerEventsCheck: 0 },
)

await expectRedirect('/note/2')
Expand All @@ -88,17 +83,15 @@ export const SavingExistingNoteShouldUpdateDBAndRedirect: Story = {
}

export const DeleteNoteRemovesFromDBAndSidebar: Story = {
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)

play: async ({ canvas }) => {
await expect(
await db.note.findMany({ where: { id: 2 } }),
'Note with id 2 does exist',
).toHaveLength(1)

await userEvent.click(
await canvas.findByRole('menuitem', { name: /delete/i }),
{ pointerEventsCheck: 0 }
{ pointerEventsCheck: 0 },
)

await expectRedirect('/')
Expand Down
20 changes: 11 additions & 9 deletions app/note/edit/page.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/**
* @vitest-environment jsdom
*/

import { expect, userEvent, within } from '@storybook/test'
import { expect, userEvent } from '@storybook/test'
import { type Meta, type StoryObj } from '@storybook/react'
import { cookies } from '@storybook/nextjs/headers.mock'
import Page from './page'
import { db } from '#lib/db'
import { createUserCookie, userCookieKey } from '#lib/session'
import { PageDecorator } from '#.storybook/decorators'
import { expectRedirect } from '#lib/test-utils'
import EditSkeleton from '#app/note/edit/loading'

const meta = {
component: Page,
Expand Down Expand Up @@ -48,8 +45,7 @@ type Story = StoryObj<typeof meta>
export const EditNewNote: Story = {}

export const SaveNewNote: Story = {
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
play: async ({ canvas }) => {
const titleInput = await canvas.findByLabelText(
'Enter a title for your note',
)
Expand All @@ -58,14 +54,16 @@ export const SaveNewNote: Story = {
)
await userEvent.clear(titleInput)
// WORKAROUND: FALSE_POSITIVE_POINTER_EVENTS
await userEvent.type(titleInput, 'New Note Title', { pointerEventsCheck: 0 })
await userEvent.type(titleInput, 'New Note Title', {
pointerEventsCheck: 0,
})
// WORKAROUND: FALSE_POSITIVE_POINTER_EVENTS
await userEvent.type(bodyInput, 'New Note Body', { pointerEventsCheck: 0 })

await userEvent.click(
await canvas.findByRole('menuitem', { name: /done/i }),
// WORKAROUND: FALSE_POSITIVE_POINTER_EVENTS
{ pointerEventsCheck: 0 }
{ pointerEventsCheck: 0 },
)

await expectRedirect('/note/3')
Expand All @@ -78,3 +76,7 @@ export const SaveNewNote: Story = {
)
},
}

export const Loading: Story = {
render: () => <EditSkeleton />,
}
13 changes: 4 additions & 9 deletions components/note-ui.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* @vitest-environment jsdom
*/

import { expect, userEvent, within } from '@storybook/test'
import { expect, userEvent } from '@storybook/test'
import { type Meta, type StoryObj } from '@storybook/react'
import { deleteNote, saveNote } from '#app/actions.mock'
import NoteUI from '#components/note-ui'
Expand All @@ -13,8 +9,8 @@ const meta = {
component: NoteUI,
async beforeEach() {
getUserFromSession.mockReturnValue('storybookjs')
saveNote.mockImplementation(async () => { })
deleteNote.mockImplementation(async () => { })
saveNote.mockImplementation(async () => {})
deleteNote.mockImplementation(async () => {})
},
} satisfies Meta<typeof NoteUI>

Expand All @@ -37,8 +33,7 @@ export const SaveAndDeleteShouldTriggerActions: Story = {
isEditing: true,
note: notes[0]!,
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
play: async ({ canvas, step }) => {
const titleInput = await canvas.findByLabelText(
'Enter a title for your note',
)
Expand Down
21 changes: 7 additions & 14 deletions components/search.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* @vitest-environment jsdom
*/

import { getRouter } from '@storybook/nextjs/navigation.mock'
import { type Meta, type StoryObj } from '@storybook/react'
import Search from './search'
import { expect, fireEvent, userEvent, within } from '@storybook/test'
import { expect, fireEvent } from '@storybook/test'

const meta = {
component: Search,
Expand All @@ -16,10 +12,9 @@ export default meta
type Story = StoryObj<typeof meta>
export const Default: Story = {}

export const WithInput: Story = {
export const WithInput = {
name: 'With input ▶️',
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
play: async ({ canvas, step }) => {
const input = canvas.getByRole('textbox')

await step('Search', async () => {
Expand All @@ -29,15 +24,13 @@ export const WithInput: Story = {
)
})
},
}
} satisfies Story

export const InputCleared: Story = {
name: 'Input cleared ▶️',
play: async (playContext) => {
await WithInput.play!(playContext)

const { canvasElement, step } = playContext
const canvas = within(canvasElement)
play: async ({ context, canvas, step }) => {
// eslint-disable-next-line storybook/context-in-play-function
await WithInput.play(context)
const input = canvas.getByRole('textbox')

getRouter().replace.mockClear()
Expand Down
10 changes: 4 additions & 6 deletions components/sidebar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useState, useEffect } from 'react'
import { useEffect, useState } from 'react'
import { type Meta, type StoryObj } from '@storybook/react'
import Sidebar from './sidebar'
import { createNotes } from '#mocks/notes'
import { expect, fireEvent, userEvent, within, waitFor } from '@storybook/test'
import { expect, userEvent, waitFor } from '@storybook/test'

const meta = {
component: Sidebar,
Expand All @@ -23,8 +23,7 @@ export const Empty: Story = {
}

export const NotesExpanded: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement)
play: async ({ canvas }) => {
const expanders = canvas.getAllByAltText(/expand/i)

expanders.forEach(async (expander) => {
Expand Down Expand Up @@ -70,8 +69,7 @@ export const ToggleSidebarOnMobile: Story = {
// because the components dimensions are not calculatable in the
// virtual DOM. This is a limitation of the virtual DOM
tags: ['!test'],
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
play: async ({ canvas, step }) => {
const searchInput = canvas.getByRole('menubar')

await step('Sidebar is initially visible', async () => {
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@
"prisma:setup": "dotenv -e .env.local prisma migrate dev && pnpm run generate-dmmf",
"prisma:seed": "dotenv -e .env.local prisma db seed",
"prisma:studio": "dotenv -e .env.local prisma studio",
"storybook:webpack": "storybook dev -p 6006",
"storybook": "storybook dev -p 6006 -c .storybook-vite",
"storybook": "storybook dev -p 6006",
"storybook:vite": "storybook dev -p 6006 -c .storybook-vite",
"build-storybook": "storybook build",
"test-storybook": "test-storybook",
"test-storybook:coverage": "test-storybook --coverage && DEBUG='istanbuljs' nyc report --reporter=text --reporter=lcov -t ./coverage/storybook --report-dir ./coverage/storybook",
"test-storybook:ci": "concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"pnpm run build-storybook --quiet && http-server storybook-static --port 6006 --silent\" \"wait-on http://127.0.0.1:6006/ && pnpm run test-storybook\"",
"generate-dmmf": "tsx prisma/generate-dmmf.ts"
},
"prisma": {
Expand Down

0 comments on commit 5706cb8

Please sign in to comment.