Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add byTestId helper #154

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions client/src/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import createFetchMock from 'vitest-fetch-mock'
import { vi } from 'vitest'
const fetchMocker = createFetchMock(vi)
import { VueWrapper } from '@vue/test-utils'

fetchMocker.enableMocks()

HTMLCanvasElement.prototype.getContext = () => {
// return whatever getContext has to return
}

// Vue Wrapper helpers

VueWrapper.prototype.byTestId = function (this: VueWrapper, id: string) {
return this.find(`[data-testid="${id}"]`)
}
7 changes: 7 additions & 0 deletions client/src/__tests__/test-extensions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { VueWrapper } from '@vue/test-utils' // eslint-disable-line

declare module '@vue/test-utils' {
interface VueWrapper<VM, T> {
byTestId(id: string): VueWrapper<VM, T>;
}
}
22 changes: 11 additions & 11 deletions client/src/components/__tests__/Notifications.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/** @vitest-environment jsdom */

import { beforeEach, describe, expect, it } from 'vitest'
import { DOMWrapper, VueWrapper, mount } from '@vue/test-utils'
import { VueWrapper, mount } from '@vue/test-utils'
import Notifications from '@components/Notifications.vue'
import { notifications } from '@client/state/notificationState'
import { nextTick } from 'vue'
import { CheckCircleIcon, ExclamationTriangleIcon, InformationCircleIcon, XCircleIcon } from '@heroicons/vue/24/solid'

let wrapper: VueWrapper
let notification: DOMWrapper<Element>
let notification: VueWrapper

describe('default', () => {
beforeEach(() => {
wrapper = mount(Notifications)
notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('does not display message by default', () => {
Expand All @@ -26,7 +26,7 @@ describe('show message with default type', () => {
wrapper = mount(Notifications)
notifications.open('Hello World')
await nextTick()
notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('should show notifications', () => {
Expand All @@ -51,7 +51,7 @@ describe('show message with success type', () => {
wrapper = mount(Notifications)
notifications.open('Hello World', 'success')
await nextTick()
notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('should display message with correct style', () => {
Expand All @@ -68,7 +68,7 @@ describe('show message with warning type', () => {
wrapper = mount(Notifications)
notifications.open('Hello World', 'warning')
await nextTick()
notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('should display message with correct style', () => {
Expand All @@ -85,7 +85,7 @@ describe('show message with error type', () => {
wrapper = mount(Notifications)
notifications.open('Hello World', 'error')
await nextTick()
notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('should display message with correct style', () => {
Expand All @@ -106,12 +106,12 @@ describe('message open', () => {
it('should close message', async () => {
const wrapper = mount(Notifications)

const close = wrapper.find('[data-testid="notifications-close"]')
const close = wrapper.byTestId('notifications-close')
close.trigger('click')

await nextTick()

expect(wrapper.find('[data-testid="notifications"]').exists()).toBe(false)
expect(wrapper.byTestId('notifications').exists()).toBe(false)
})
})
})
Expand All @@ -124,7 +124,7 @@ describe('Message called via query param', () => {
wrapper = mount(Notifications)
await nextTick()

notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('should display info message', async () => {
Expand All @@ -142,7 +142,7 @@ describe('Message called via query param with type', () => {
wrapper = mount(Notifications)
await nextTick()

notification = wrapper.find('[data-testid="notifications"]')
notification = wrapper.byTestId('notifications')
})

it('should display success message', async () => {
Expand Down
17 changes: 7 additions & 10 deletions client/src/components/__tests__/ReminderSubscribe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { nextTick } from 'vue'

let wrapper: VueWrapper

const subscribeSelector = '[data-testid="subscribe-button"]'
const getRemindersSelector = '[data-testid="get-reminders-button"]'

globalThis.fetch = vi.fn()

beforeAll(() => {
Expand All @@ -25,7 +22,7 @@ beforeEach(() => {
})

it('renders "Get Reminders" button with unopened style', () => {
const button = wrapper.find(getRemindersSelector)
const button = wrapper.byTestId('get-reminders-button')

expect(button.text()).toBe('Get Reminders!')
expect(button.classes()).toContain('text-white')
Expand All @@ -38,7 +35,7 @@ describe('press the "Get Reminders" button', () => {
})

it('renders "Get Reminders" button with opened style', () => {
const button = wrapper.find(getRemindersSelector)
const button = wrapper.byTestId('get-reminders-button')

expect(button.text()).toBe('Get Reminders!')
expect(button.classes()).toContain('text-mint')
Expand All @@ -50,7 +47,7 @@ describe('press the "Get Reminders" button', () => {
})

it('shows a "Subscribe" button', () => {
expect(wrapper.find(subscribeSelector).exists()).toBe(true)
expect(wrapper.byTestId('subscribe-button').exists()).toBe(true)
})

describe('enter email and subscribe with success', () => {
Expand All @@ -63,7 +60,7 @@ describe('press the "Get Reminders" button', () => {
})

await wrapper.find('input').setValue('test@example.com')
await wrapper.find(subscribeSelector).trigger('click')
await wrapper.byTestId('subscribe-button').trigger('click')
await nextTick()
})

Expand All @@ -77,7 +74,7 @@ describe('press the "Get Reminders" button', () => {

it('closes the input', async () => {
expect(wrapper.find('input').exists()).toBe(false)
expect(wrapper.find(subscribeSelector).exists()).toBe(false)
expect(wrapper.byTestId('subscribe-button').exists()).toBe(false)
})
})

Expand All @@ -92,12 +89,12 @@ describe('press the "Get Reminders" button', () => {
})

await wrapper.find('input').setValue('test@example.com')
await wrapper.find(subscribeSelector).trigger('click')
await wrapper.byTestId('subscribe-button').trigger('click')
})

it('does not close the input', async () => {
expect(wrapper.find('input').exists()).toBe(true)
expect(wrapper.find(subscribeSelector).exists()).toBe(true)
expect(wrapper.byTestId('subscribe-button').exists()).toBe(true)
})
})
})
16 changes: 8 additions & 8 deletions client/src/components/__tests__/RsvpModal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('name and email already set', () => {
rsvpModal.open(week)
const wrapper = mount(RsvpModal)

expect(wrapper.find('[data-testid="input-name"]').element.value).toEqual('John Doe')
expect(wrapper.find('[data-testid="input-email"]').element.value).toEqual('jdoe@example.com')
expect(wrapper.byTestId('input-name').element.value).toEqual('John Doe')
expect(wrapper.byTestId('input-email').element.value).toEqual('jdoe@example.com')
})
})

Expand All @@ -34,7 +34,7 @@ describe('nothing input', () => {
rsvpModal.open(week)
const wrapper = mount(RsvpModal)

expect(wrapper.find('[data-testid="rsvp-button"]').attributes('disabled')).toBe('')
expect(wrapper.byTestId('rsvp-button').attributes('disabled')).toBe('')
})
})

Expand All @@ -44,9 +44,9 @@ describe('only name input', () => {
rsvpModal.open(week)
const wrapper = mount(RsvpModal)

await wrapper.find('[data-testid="input-name"]').setValue('John Doe')
await wrapper.byTestId('input-name').setValue('John Doe')

expect(wrapper.find('[data-testid="rsvp-button"]').attributes('disabled')).toBe(undefined)
expect(wrapper.byTestId('rsvp-button').attributes('disabled')).toBe(undefined)
})
})

Expand All @@ -62,10 +62,10 @@ describe('rsvp submit', () => {
})
rsvpModal.open(week)
const wrapper = mount(RsvpModal)
await wrapper.find('[data-testid="input-name"]').setValue('John Doe')
await wrapper.find('[data-testid="input-email"]').setValue('jdoe@example.com')
await wrapper.byTestId('input-name').setValue('John Doe')
await wrapper.byTestId('input-email').setValue('jdoe@example.com')

await wrapper.find('[data-testid="rsvp-button"]').trigger('click')
await wrapper.byTestId('rsvp-button').trigger('click')
})

it('calls api with the correct data', async () => {
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/week/__tests__/Theme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Styled theme', () => {
props: { week },
})

const component = wrapper.find('[data-testid="theme"]')
const component = wrapper.byTestId('theme')
expect(component.exists()).toBe(true)
expect(component.element.children[0].innerHTML).toBe('The')
expect(component.element.children[0].classList).toContain('font-bold')
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('Week skipped', () => {
props: { week },
})

const component = wrapper.find('[data-testid="theme"]')
const component = wrapper.byTestId('theme')
expect(component.exists()).toBe(true)
expect(component.element.children[0].innerHTML).toBe('The')
expect(component.element.children[0].classList).toContain('font-bold')
Expand Down
Loading