From 43cc966698b08c118978adef0767e7ea27283842 Mon Sep 17 00:00:00 2001 From: Shohzod Irgas Date: Wed, 27 Nov 2024 18:33:36 +0300 Subject: [PATCH 1/9] migrated all the tests to rtl. --- .dockerignore | 4 + dockerfile | 20 +++++ src/tests/react-testing-library.test.tsx | 94 ++++++++++++++++++++++-- 3 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 .dockerignore create mode 100644 dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c0dc144 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +node_modules +npm-debug.log +Dockerfile +.dockerignore \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..3cd203d --- /dev/null +++ b/dockerfile @@ -0,0 +1,20 @@ +# Use the official Node.js image from the Docker Hub and specify the version +FROM node:16.10.1 + +# Create and set the working directory inside the container +WORKDIR /usr/src/app + +# Copy the package.json and package-lock.json files +COPY package*.json ./ + +# Install the project dependencies +RUN yarn + +# Copy the rest of the project files +COPY . . + +# Expose the port the app runs on (change this if your app uses a different port) +EXPOSE 3000 + +# Command to run the application +CMD ["npm", "start"] \ No newline at end of file diff --git a/src/tests/react-testing-library.test.tsx b/src/tests/react-testing-library.test.tsx index 1627974..d1bac2e 100644 --- a/src/tests/react-testing-library.test.tsx +++ b/src/tests/react-testing-library.test.tsx @@ -1,34 +1,114 @@ import React from 'react'; +import { fireEvent, logRoles } from '@testing-library/dom'; import { render, screen } from '@testing-library/react'; import { App } from '../components/app'; +import { Toggle, ToggleProps } from '../components/toggle/toggle'; +import { Button, ButtonProps } from '../components/button/button'; describe('React Testing Library do:', () => { test('create snapshot of mounted component', () => { - throw new Error('Not migrated yet'); + // render app + render(); + + // check the snapshot + expect(screen).toMatchSnapshot; }); test('find child by component', () => { - throw new Error('Not migrated yet'); + // render App + render(); + + + // find the component + const childElement = screen.getByText(/click/i); + + // check if it exists + expect(childElement).toBeInTheDocument(); }); test('find child by attribute', () => { - throw new Error('Not migrated yet'); + // render App + const {container} = render(); + + // find the component + const childElement = container.querySelector('[data-test-target="button-element"]'); // had to use query selector because getByTestId doesn't work since that function requires a data attribute named data-testid. + + // check if it exists + expect(childElement).toBeInTheDocument(); }); test('find child by class name', () => { - throw new Error('Not migrated yet'); + // render component + const props: ToggleProps = { + initialState: true, + title: 'Dummy toggle title', + onChange: () => null, + }; + const {container} = render(); + + + // find the component + const input = container.querySelector('.toggle-style'); + + // check if it exists + expect(input).toBeInTheDocument(); }); test('get child attribute', () => { - throw new Error('Not migrated yet'); + // render app + render(); + + // find the component and it's attribute + const input = screen.getByLabelText(/toggle/i); + + // simulate input + fireEvent.click(input); + + // check the attribute value (since the initial state of the checkbox is checked, im asserting wether or not it is unchecked.) + expect(input).not.toBeChecked(); }); test('set component property', () => { - throw new Error('Not migrated yet'); + // create a dummy button + const props: ButtonProps = { + title: "Dummy button title", + isDisabled: false, + onClick: jest.fn(), + } + + // render and disable the button + const {rerender} = render(