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

Remove jQuery from Instructor List (Fix #777 Part 1/2) #806

Merged
merged 8 commits into from
Jun 2, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "12.1.5",
"@testing-library/user-event": "14.5.2",
"babel-jest": "^26.6.0",
"cross-env": "^7.0.2",
"eslint-config-airbnb": "^18.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { LanguageConstants } from 'components/CTPlayer';
import NewEPubModal from './NewEPubModal';

Expand All @@ -14,7 +14,8 @@ describe('New EPubModal', () => {
onClose: jest.fn(),
onCreate: jest.fn(),
defaultTitle: ""
}
};

test('it renders with no languages', () => {
render(<NewEPubModal {...baseProps} />);

Expand Down
8 changes: 5 additions & 3 deletions src/screens/Admin/Instructors/InstructorList.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useState, useEffect } from 'react';
import $ from 'jquery';
import { Button } from 'semantic-ui-react';
import { search } from 'utils';
import * as KeyCode from 'keycode-js';
import { AdminListItem } from '../Components';

export default function InstructorList({ instructors, loading, currUni, onInactive }) {
const [result, setResult] = useState([]);
const [searchText, setSearchText] = useState("");

useEffect(() => {
setResult(instructors);
Expand All @@ -15,7 +15,7 @@ export default function InstructorList({ instructors, loading, currUni, onInacti
const onSearch = (keyCode) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

@angrave if we want, we can make the search happen 'live' with every state change. That way we won't need to even hit enter or click the button now that we're not using jQuery

Copy link
Member Author

Choose a reason for hiding this comment

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

This can be a separate commit actually, I'll make this into a new issue so that this PR is fixed on just removing jquery.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

if (keyCode === KeyCode.KEY_RETURN) {
setResult(
search.getResults(instructors, $('#inst-filter')[0].value, [
search.getResults(instructors, searchText, [
'firstName',
'lastName',
'email',
Expand All @@ -26,7 +26,7 @@ export default function InstructorList({ instructors, loading, currUni, onInacti

const onReset = () => {
setResult(instructors);
$('#inst-filter')[0].value = '';
setSearchText('');
};

return (
Expand All @@ -36,6 +36,8 @@ export default function InstructorList({ instructors, loading, currUni, onInacti
<input
id="inst-filter"
placeholder="Name or email"
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
onKeyDown={({ keyCode }) => {
return onSearch(keyCode);
}}
Expand Down
79 changes: 79 additions & 0 deletions src/screens/Admin/Instructors/InstructorList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { render, screen } from '@testing-library/react';
import userEvent from "@testing-library/user-event";
import * as KeyCode from 'keycode-js';
import InstructorList from './InstructorList';

describe('Instructor List', () => {
const universityName = 'University of Test'
const baseProps = {
loading: false,
currUni: { name: universityName, id: 0 },
onInactive: jest.fn()
};

// All instructors have the same univeristy display in common
const numberOfInstructors = () => screen.queryAllByText(`University: ${universityName}`).length

test('it renders', () => {
render(<InstructorList {...baseProps} instructors={[]} />);

expect(numberOfInstructors()).toBe(0);
});

// Note: only setting up fields we use for display, add more as needed
const instructors = [
{ id: 0, firstName: "Harsh", lastName: "Deep", email: "harsh@example.com" },
{ id: 1, firstName: "Alan", email: "alan@example.com" },
{ id: 2, lastName: "Turing", email: "turing@example.com" },
]

test('it shows all instructors', () => {
render(<InstructorList {...baseProps} instructors={instructors} />);

expect(numberOfInstructors()).toBe(3);

expect(screen.getByText("Harsh Deep")).toBeVisible();
expect(screen.getByText("Alan")).toBeVisible();
expect(screen.getByText("Unknown Turing")).toBeVisible();

expect(screen.getByText("Email: harsh@example.com")).toBeVisible();
expect(screen.getByText("Email: alan@example.com")).toBeVisible();
expect(screen.getByText("Email: harsh@example.com")).toBeVisible();
});

test('it filters based on first, last and email and we can reset the filters', async () => {
render(<InstructorList {...baseProps} instructors={instructors} />);

const searchField = screen.getByLabelText("Search:");
const searchButton = screen.getByRole("button", { name: "search"});
const resetButton = screen.getByRole("button", { name: "Reset" });

// First Name
await userEvent.type(searchField, "Harsh");
await userEvent.click(searchButton);

expect(numberOfInstructors()).toBe(1);
expect(screen.getByText("Harsh Deep")).toBeVisible();

// Last Name
await userEvent.clear(searchField);
await userEvent.type(searchField, "Turing");
await userEvent.click(searchButton);

expect(numberOfInstructors()).toBe(1);
expect(screen.getByText("Unknown Turing")).toBeVisible();

// Email
await userEvent.clear(searchField);
await userEvent.type(searchField, "alan@example");
await userEvent.click(searchButton);

expect(numberOfInstructors()).toBe(1);
expect(screen.getByText("Alan")).toBeVisible();

// Reset
await userEvent.click(resetButton);
expect(searchField.value).toBe("");
expect(numberOfInstructors()).toBe(3);
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3068,6 +3068,11 @@
"@testing-library/dom" "^8.0.0"
"@types/react-dom" "<18.0.0"

"@testing-library/user-event@14.5.2":
version "14.5.2"
resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-14.5.2.tgz#db7257d727c891905947bd1c1a99da20e03c2ebd"
integrity sha512-YAh82Wh4TIrxYLmfGcixwD18oIjyC1pFQC2Y01F2lzV2HTMiYrI0nze0FD0ocB//CKS/7jIUgae+adPqxK5yCQ==

"@tootallnate/once@1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
Expand Down
Loading