Skip to content

Commit

Permalink
test: add component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdehaven committed Jan 3, 2024
1 parent 0ca9295 commit e5dfe58
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/components/MarkdownUi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('@vueuse/core', async () => {
const actual: any = await vi.importActual('@vueuse/core')
return {
...actual,
// Mock the useScroll composable
useScroll: vi.fn(() => ({
arrivedState: {
left: false,
Expand All @@ -28,7 +29,7 @@ const defaultContent = `# ${defaultText}`

// The markdown content takes roughly 350ms to initialize, so wait in each test.
// Not ideal.
const waitForContent = async (timeout = 350): Promise<void> => await new Promise((resolve) => setTimeout(resolve, timeout))
const waitForContent = async (timeout = 300): Promise<void> => await new Promise((resolve) => setTimeout(resolve, timeout))

// Stub the return value for useMediaQuery to determine if browser is at least phablet width
const mediaQuerySpy = ({
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('<MarkdownUi />', () => {
})

// No need to wait in edit mode
await waitForContent()
await waitForContent(0)

expect(wrapper.findTestId('markdown-ui').isVisible()).toBe(true)
expect(wrapper.findTestId('markdown-content').isVisible()).toBe(true)
Expand All @@ -132,7 +133,7 @@ describe('<MarkdownUi />', () => {
})

// No need to wait in edit mode
await waitForContent()
await waitForContent(0)

expect(wrapper.findTestId('markdown-ui').isVisible()).toBe(true)
expect(wrapper.findTestId('toolbar').isVisible()).toBe(true)
Expand All @@ -150,13 +151,54 @@ describe('<MarkdownUi />', () => {
})
})

// describe('split', () => {
describe('split', () => {
it('displays both the editor and preview panes with correct content', async () => {
const text = '# Starter content'
const wrapper = mount(MarkdownUi, {
props: {
mode: 'split',
editable: true,
modelValue: text,
},
})

await waitForContent()

expect(wrapper.findTestId('markdown-ui').isVisible()).toBe(true)
expect(wrapper.findTestId('toolbar').isVisible()).toBe(true)
expect(wrapper.findTestId('markdown-editor-textarea').isVisible()).toBe(true)
expect(wrapper.findTestId('markdown-content').isVisible()).toBe(true)
// Expect text
expect((wrapper.findTestId('markdown-editor-textarea').element as HTMLTextAreaElement).value).toEqual(text)
expect(wrapper.findTestId('markdown-content').text()).toContain(text.replace('# ', ''))
// Elements should not exist
expect(wrapper.findTestId('edit').exists()).toBe(false)
})
})

// })
describe('preview', () => {
it('displays only the preview pane correct content', async () => {
const text = '# Starter content'
const wrapper = mount(MarkdownUi, {
props: {
mode: 'preview',
editable: true,
modelValue: text,
},
})

// describe('preview', () => {
await waitForContent()

// })
expect(wrapper.findTestId('markdown-ui').isVisible()).toBe(true)
expect(wrapper.findTestId('toolbar').isVisible()).toBe(true)
expect(wrapper.findTestId('markdown-content').isVisible()).toBe(true)
// Expect text
expect(wrapper.findTestId('markdown-content').text()).toContain(text.replace('# ', ''))
// Elements should not exist
expect(wrapper.findTestId('markdown-editor-textarea').exists()).toBe(false)
expect(wrapper.findTestId('edit').exists()).toBe(false)
})
})
})

describe('actions', () => {
Expand Down

0 comments on commit e5dfe58

Please sign in to comment.