Skip to content

Commit

Permalink
rewrite test with faker
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-takei committed Sep 24, 2024
1 parent f5101dd commit 80e9fda
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions apps/app/src/client/components/PageHeader/PageTitleHeader.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import '@testing-library/jest-dom/vitest';

import { faker } from '@faker-js/faker';
import type { IPagePopulatedToShowRevision } from '@growi/core';
import {
fireEvent, render, screen, waitFor,
} from '@testing-library/react';
import { mock } from 'vitest-mock-extended';


import { EditorMode } from '~/stores-universal/ui';

import { PageTitleHeader } from './PageTitleHeader';
Expand All @@ -32,7 +34,7 @@ describe('PageTitleHeader Component with untitled page', () => {
it('should render the textbox correctly', async() => {
// arrange
const currentPage = mock<IPagePopulatedToShowRevision>({
_id: 'dummy-id',
_id: faker.database.mongodbObjectId(),
path: '/path/to/page/Untitled-1',
});

Expand Down Expand Up @@ -63,17 +65,18 @@ describe('PageTitleHeader Component', () => {

it('should render the title correctly', async() => {
// arrange
const pageTitle = faker.lorem.slug();
const currentPage = mock<IPagePopulatedToShowRevision>({
_id: 'dummy-id',
path: '/path/to/page/page-title',
_id: faker.database.mongodbObjectId(),
path: `/path/to/page/${pageTitle}`,
});

// act
render(<PageTitleHeader currentPage={currentPage} />);

// assert
// header should be rendered
const headerElement = screen.getByText('page-title');
const headerElement = screen.getByText(pageTitle);
await waitFor(() => {
expect(headerElement).toBeInTheDocument();
expect(headerElement).not.toHaveClass('invisible');
Expand All @@ -85,15 +88,16 @@ describe('PageTitleHeader Component', () => {

it('should render text input after clicking', async() => {
// arrange
const pageTitle = faker.lorem.slug();
const currentPage = mock<IPagePopulatedToShowRevision>({
_id: 'dummy-id',
path: '/path/to/page/page-title',
_id: faker.database.mongodbObjectId(),
path: `/path/to/page/${pageTitle}`,
});

// act
render(<PageTitleHeader currentPage={currentPage} />);

const headerElement = screen.getByText('page-title');
const headerElement = screen.getByText(pageTitle);
await waitFor(() => expect(headerElement).toBeInTheDocument());

// click
Expand All @@ -103,7 +107,7 @@ describe('PageTitleHeader Component', () => {
const inputElement = screen.getByRole('textbox');
await waitFor(() => {
expect(inputElement).toBeInTheDocument();
expect(inputElement).toHaveValue('page-title');
expect(inputElement).toHaveValue(pageTitle);
expect(headerElement).toHaveClass('invisible');
});
});
Expand Down

0 comments on commit 80e9fda

Please sign in to comment.