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

Initial UI testing with Jest #70

Merged
merged 7 commits into from
Jan 26, 2024
Merged

Initial UI testing with Jest #70

merged 7 commits into from
Jan 26, 2024

Conversation

shivaypiece
Copy link
Contributor

3 initial tests:

  1. test using Jest to see if the label of the button component is ‘Create Snippet’:
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { CreateButton } from '../Button';
import '@testing-library/jest-dom'

describe('CreateButton', () => {
  it('should have "Create Snippet" as button label', () => {
    render(<CreateButton applicationData={null} data={null} name={null} />);
    const buttonElement = screen.getByRole('button', { name: /Create Snippet/i });
    expect(buttonElement).toBeInTheDocument();
  });
});
  1. test to check if nameInput and DataText area exist in the TextInput component
  test('renders input and textarea elements', () => {
    render(<DataTextInput applicationData={null} />);

    const nameInput = screen.getByPlaceholderText('Type the name of your snippet...') as HTMLInputElement;
    const dataTextarea = screen.getByPlaceholderText('Add your code/text content into this box') as HTMLTextAreaElement;

    expect(nameInput).toBeInTheDocument();
    expect(dataTextarea).toBeInTheDocument();
  });
  1. test to check if there is an update in the state when input and the textarea values change.
  test('updates the state when input and textarea values change', () => {
    render(<DataTextInput applicationData={null} />);

    const nameInput = screen.getByPlaceholderText('Type the name of your snippet...') as HTMLInputElement;
    const dataTextarea = screen.getByPlaceholderText('Add your code/text content into this box') as HTMLTextAreaElement;

    // Simulate user input and check if the state is updated accordingly
    expect(nameInput.value).toEqual('');
    fireEvent.change(nameInput, { target: { value: 'Snippet name' } });
    expect(nameInput.value).toEqual('Snippet name');

    expect(dataTextarea.value).toEqual('');
    fireEvent.change(dataTextarea, { target: { value: 'Snippet code' } });
    expect(dataTextarea.value).toEqual('Snippet code');
  });

@shivaypiece shivaypiece changed the title Initial UI testing Initial UI testing with Jest Jan 18, 2024
@shivaypiece shivaypiece requested a review from jwafu January 18, 2024 23:29
@shivaypiece shivaypiece marked this pull request as ready for review January 23, 2024 16:58
Copy link
Contributor

@jwafu jwafu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious here let me know what you think

tsconfig.json Outdated
@@ -13,5 +13,5 @@
/* Skip type checking all .d.ts files. */
"jsx": "react"
},
"exclude": ["node_modules","jest.config.js"]
"exclude": ["node_modules","jest.config.js", "/Users/shivaylamba/Desktop/example-ts/__mocks__"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice addition here 👍 i wonder if there is some way to make this a variable so we dont have to exclude this, otherwise i believe we will have to add everyones path here for running tests - no? @shivay-at-pieces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated this to include any directories including mock, so this should work for others as well.

@shivaypiece shivaypiece requested a review from jwafu January 24, 2024 01:59
@jwafu jwafu merged commit e636ac9 into main Jan 26, 2024
1 check passed
@shivaypiece shivaypiece deleted the testing branch March 5, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants