Skip to content

Commit

Permalink
modify config and add sample component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
git-nandor committed May 14, 2024
1 parent f6e294a commit 07ce784
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 32 deletions.
5 changes: 3 additions & 2 deletions packages/ui-avatar/src/Avatar/__new-tests__/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
import React from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import { runAxeCheck } from '@instructure/ui-axe-check'

import '@testing-library/jest-dom'
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('<Avatar />', () => {
})

it('should return the underlying component', async () => {
const elementRef = jest.fn()
const elementRef = vi.fn()
const { container } = render(
<Avatar name="Avatar Name" elementRef={elementRef} />
)
Expand Down Expand Up @@ -137,7 +138,7 @@ describe('<Avatar />', () => {
})

it('should call onImageLoaded once the image loads', async () => {
const onImageLoaded = jest.fn()
const onImageLoaded = vi.fn()
const { container } = render(
<Avatar name="Avatar Name" onImageLoaded={onImageLoaded} />
)
Expand Down
15 changes: 8 additions & 7 deletions packages/ui-dialog/src/Dialog/__new-tests__/Dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import React, {
useState
} from 'react'
import { fireEvent, render, waitFor } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import { userEvent } from '@testing-library/user-event'
import '@testing-library/jest-dom'

Expand Down Expand Up @@ -156,7 +157,7 @@ describe('<Dialog />', () => {
})

it('should call onDismiss prop when Esc key pressed', async () => {
const onDismiss = jest.fn()
const onDismiss = vi.fn()
const { getByRole } = renderDialog({ onDismiss })
const dialog = getByRole('dialog')

Expand All @@ -172,7 +173,7 @@ describe('<Dialog />', () => {
})

it('should call onDismiss prop when the document is clicked', async () => {
const onDismiss = jest.fn()
const onDismiss = vi.fn()
renderDialog({ onDismiss, shouldCloseOnDocumentClick: true })

await waitFor(() => {
Expand Down Expand Up @@ -205,7 +206,7 @@ describe('<Dialog />', () => {
})

it('should warn when trying to focus or blur a closed dialog', () => {
const consoleError = jest
const consoleError = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
let ref
Expand Down Expand Up @@ -333,7 +334,7 @@ describe('<Dialog />', () => {

describe('when focus leaves the first and last tabbable', () => {
it(`should NOT call onBlur when shouldContainFocus=true and tab pressing last tabbable`, async () => {
const onBlur = jest.fn()
const onBlur = vi.fn()
const { getByTestId } = render(
<DialogExample
open
Expand All @@ -357,7 +358,7 @@ describe('<Dialog />', () => {
})

it('should NOT call onBlur when shouldContainFocus=true and Shift+Tab pressing first tabbable', async () => {
const onBlur = jest.fn()
const onBlur = vi.fn()

const { getByTestId } = render(
<DialogExample
Expand Down Expand Up @@ -387,7 +388,7 @@ describe('<Dialog />', () => {
})

it('should call onBlur when shouldContainFocus=false and tab pressing last tabbable', async () => {
const onBlur = jest.fn()
const onBlur = vi.fn()

const { getByTestId } = render(
<DialogExample
Expand All @@ -414,7 +415,7 @@ describe('<Dialog />', () => {
})

it('should call onBlur when shouldContainFocus=false and pressing Shift+Tab on the first tabbable', async () => {
const onBlur = jest.fn()
const onBlur = vi.fn()

const { getByTestId } = render(
<DialogExample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import React from 'react'

import { render } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import '@testing-library/jest-dom'

import { color2hex } from '@instructure/ui-color-utils'
Expand Down Expand Up @@ -85,7 +86,7 @@ describe('<ModalBody />', () => {
allProps.forEach((prop) => {
if (prop in allowedProps) {
it(`should allow the '${prop}' prop`, () => {
const consoleErrorSpy = jest
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
const props = { [prop]: allowedProps[prop] }
Expand All @@ -98,7 +99,7 @@ describe('<ModalBody />', () => {
} else {
it(`should NOT allow the '${prop}' prop`, () => {
const expectedErrorMessage = `prop '${prop}' is not allowed.`
const consoleErrorSpy = jest
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import React from 'react'

import { render } from '@testing-library/react'
import { describe, it, expect } from 'vitest'
import '@testing-library/jest-dom'

import { canvas } from '@instructure/ui-themes'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import React from 'react'

import { render } from '@testing-library/react'
import { describe, it, expect } from 'vitest'
import '@testing-library/jest-dom'

import { canvas } from '@instructure/ui-themes'
Expand Down
25 changes: 13 additions & 12 deletions packages/ui-modal/src/Modal/__new-tests__/Modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import React from 'react'
import { render, waitFor } from '@testing-library/react'
import { describe, it, expect, afterEach, vi } from 'vitest'
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom'

Expand All @@ -32,8 +33,8 @@ import type { ModalProps } from '../props'

describe('<Modal />', () => {
afterEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
vi.clearAllMocks()
vi.restoreAllMocks()
})

it('should render nothing and have a node with no parent when closed', () => {
Expand Down Expand Up @@ -154,9 +155,9 @@ describe('<Modal />', () => {
})

it('should use transition', async () => {
const onEnter = jest.fn()
const onEntering = jest.fn()
const onEntered = jest.fn()
const onEnter = vi.fn()
const onEntering = vi.fn()
const onEntered = vi.fn()

const { findByRole } = render(
<Modal
Expand All @@ -183,7 +184,7 @@ describe('<Modal />', () => {
})

it('should support onOpen prop', async () => {
const onOpen = jest.fn()
const onOpen = vi.fn()
const { findByRole } = render(
<Modal
open
Expand All @@ -204,7 +205,7 @@ describe('<Modal />', () => {
})

it('should support onClose prop', async () => {
const onClose = jest.fn()
const onClose = vi.fn()

const { findByRole, rerender } = render(
<Modal
Expand Down Expand Up @@ -237,7 +238,7 @@ describe('<Modal />', () => {
})

it('should dismiss when overlay clicked by default', async () => {
const onDismiss = jest.fn()
const onDismiss = vi.fn()
const { findByText } = render(
<Modal
open
Expand All @@ -259,8 +260,8 @@ describe('<Modal />', () => {
})

it('should NOT dismiss when overlay clicked with shouldCloseOnDocumentClick=false', async () => {
const onDismiss = jest.fn()
const onClickOuter = jest.fn()
const onDismiss = vi.fn()
const onClickOuter = vi.fn()

const { findByRole, getByTestId } = render(
<div>
Expand Down Expand Up @@ -308,7 +309,7 @@ describe('<Modal />', () => {

describe('children validation', () => {
it('should pass validation when children are valid', async () => {
const consoleErrorSpy = jest
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})

Expand All @@ -330,7 +331,7 @@ describe('<Modal />', () => {
})

it('should not pass validation when children are invalid', async () => {
const consoleErrorSpy = jest
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})

Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
// "types": ["vitest/globals"],
"types": ["@testing-library/jest-dom"],
"types": ["vitest/globals", "@testing-library/jest-dom"],
"baseUrl": "./packages",
"paths": {
"@instructure/*": ["*/src"],
Expand Down
13 changes: 7 additions & 6 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/// <reference types="vitest" />

import { defineConfig } from 'vitest/config'
// import react from "@vitejs/plugin-react"

export default defineConfig({
// plugins: [react()],
test: {
//include: ['**/__new-tests__/**/*.test.tsx'],
//include: ["./packages/ui-avatar/src/Avatar/__new-tests__/*.test.tsx"],
include: ["./packages/ui-popover/src/Popover/__new-tests__/*.test.tsx"],
include: [
"./packages/ui-avatar/**/__new-tests__/**/*.test.tsx",
"./packages/ui-popover/**/__new-tests__/**/*.test.tsx",
"./packages/ui-dialog/**/__new-tests__/**/*.test.tsx",
"./packages/ui-modal/**/__new-tests__/**/*.test.tsx"],
globals: true,
environment: "jsdom",
setupFiles: './vitest/setup.js',
setupFiles: './vitest.setup.ts',
},
})
})
3 changes: 2 additions & 1 deletion vitest/setup.js → vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import '@testing-library/jest-dom/vitest'
import { cleanup } from '@testing-library/react'
import { afterEach } from 'vitest'
import { afterEach, vi } from 'vitest'

// runs a clean after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup()
vi.restoreAllMocks()
})

0 comments on commit 07ce784

Please sign in to comment.