Skip to content

Commit

Permalink
000 - Added and refactored Practice.test - fa
Browse files Browse the repository at this point in the history
  • Loading branch information
allenf3 committed Mar 1, 2022
1 parent 23ac396 commit 1786244
Showing 1 changed file with 150 additions and 104 deletions.
254 changes: 150 additions & 104 deletions web/src/components/Practice/Practice.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ const testCodeValid = {
id: 15,
};

const correctAnswerResponse = {
correct: true,
};

const incorrectResponseNoErrors = {
correct: false,
noErrors: true,
};

const incorrectResponseTwoErrors = {
correct: false,
twoErrors: true,
};

const incorrectResponseBitFlipped = {
correct: false,
flippedBit: 4,
};

const testSetup = () => {
const mockApi = new MockAdapter(axios);
mockApi.onGet(`${process.env.REACT_APP_BASE_API}/api/HammingCodes`).reply(200, testCodeValid);
Expand All @@ -25,122 +44,149 @@ const testSetupAndRender = () => {
);
};

test('practice page renders correctly', async () => {
testSetupAndRender();
expect(await screen.findByText('Practice working with Hamming codes')).toBeInTheDocument();
});

test('home link present on practice page', async () => {
testSetup();
await shouldLinkToHome(Practice);
});

test('click changes bit class', async () => {
testSetupAndRender();
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
fireEvent.click(bitToClick);
expect(bitToClick.classList.contains('selected-hamming-bit')).toBe(true);
});
describe('page is rendered', () => {
beforeEach(async () => {
testSetupAndRender();
});

test('cannot select multiple bits', async () => {
testSetupAndRender();
const bits = await screen.findAllByText('0');
const firstBit = bits[2];
const secondBit = bits[5];
expect(firstBit).toBeInTheDocument();
expect(secondBit).toBeInTheDocument();
expect(firstBit.classList.contains('hamming-bit')).toBe(true);
expect(secondBit.classList.contains('hamming-bit')).toBe(true);
expect(firstBit.classList.contains('selected-hamming-bit')).toBe(false);
expect(secondBit.classList.contains('selected-hamming-bit')).toBe(false);
fireEvent.click(firstBit);
fireEvent.click(secondBit);
expect(firstBit.classList.contains('hamming-bit')).toBe(false);
expect(secondBit.classList.contains('hamming-bit')).toBe(true);
expect(firstBit.classList.contains('selected-hamming-bit')).toBe(true);
expect(secondBit.classList.contains('selected-hamming-bit')).toBe(false);
});
test('practice page renders correctly', async () => {
expect(await screen.findByText('Practice working with Hamming codes')).toBeInTheDocument();
});

test('no errors button is in the document', async () => {
testSetupAndRender();
const noErrors = await screen.findByText('If no errors, click here');
expect(noErrors).toBeInTheDocument();
});
test('click changes bit class', async () => {
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
fireEvent.click(bitToClick);
expect(bitToClick.classList.contains('selected-hamming-bit')).toBe(true);
});

test('bits can not be selected if no errors is selected', async () => {
testSetupAndRender();
const noErrors = await screen.findByText('If no errors, click here');
fireEvent.click(noErrors);
const noErrorsSelected = await screen.findByText('No Errors');
expect(noErrorsSelected).toBeInTheDocument();
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
fireEvent.click(bitToClick);
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
});
test('cannot select multiple bits', async () => {
const bits = await screen.findAllByText('0');
const firstBit = bits[2];
const secondBit = bits[5];
expect(firstBit).toBeInTheDocument();
expect(secondBit).toBeInTheDocument();
expect(firstBit.classList.contains('hamming-bit')).toBe(true);
expect(secondBit.classList.contains('hamming-bit')).toBe(true);
expect(firstBit.classList.contains('selected-hamming-bit')).toBe(false);
expect(secondBit.classList.contains('selected-hamming-bit')).toBe(false);
fireEvent.click(firstBit);
fireEvent.click(secondBit);
expect(firstBit.classList.contains('hamming-bit')).toBe(false);
expect(secondBit.classList.contains('hamming-bit')).toBe(true);
expect(firstBit.classList.contains('selected-hamming-bit')).toBe(true);
expect(secondBit.classList.contains('selected-hamming-bit')).toBe(false);
});

test('two errors button is in the document', async () => {
testSetupAndRender();
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors).toBeInTheDocument();
});
test('no errors button is in the document', async () => {
const noErrors = await screen.findByText('If no errors, click here');
expect(noErrors).toBeInTheDocument();
});

test('bits can not be selected if two errors is selected', async () => {
testSetupAndRender();
const twoErrors = await screen.findByText('If two errors, click here');
fireEvent.click(twoErrors);
const twoErrorsSelected = await screen.findByText('Two Errors');
expect(twoErrorsSelected).toBeInTheDocument();
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
fireEvent.click(bitToClick);
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
});
test('bits can not be selected if no errors is selected', async () => {
const noErrors = await screen.findByText('If no errors, click here');
fireEvent.click(noErrors);
const noErrorsSelected = await screen.findByText('No Errors');
expect(noErrorsSelected).toBeInTheDocument();
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
fireEvent.click(bitToClick);
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
});

test('two errors button can not be selected if bit is already selected', async () => {
testSetupAndRender();
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
fireEvent.click(bitToClick);
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors.classList.contains('two-errors')).toBe(true);
fireEvent.click(twoErrors);
expect(twoErrors.classList.contains('two-errors')).toBe(true);
});
test('two errors button is in the document', async () => {
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors).toBeInTheDocument();
});

test('two errors button can not be selected if no errors button is already selected', async () => {
testSetupAndRender();
const noErrors = await screen.findByText('If no errors, click here');
expect(noErrors).toBeInTheDocument();
fireEvent.click(noErrors);
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors.classList.contains('two-errors')).toBe(true);
fireEvent.click(twoErrors);
expect(twoErrors.classList.contains('two-errors')).toBe(true);
});
test('bits can not be selected if two errors is selected', async () => {
const twoErrors = await screen.findByText('If two errors, click here');
fireEvent.click(twoErrors);
const twoErrorsSelected = await screen.findByText('Two Errors');
expect(twoErrorsSelected).toBeInTheDocument();
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
fireEvent.click(bitToClick);
expect(bitToClick.classList.contains('hamming-bit')).toBe(true);
});

test('no errors button can not be selected if two errors button is already selected', async () => {
testSetupAndRender();
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors).toBeInTheDocument();
fireEvent.click(twoErrors);
const noErrors = await screen.findByText('If no errors, click here');
expect(noErrors).toBeInTheDocument();
fireEvent.click(noErrors);
expect(noErrors.classList.contains('no-errors')).toBe(true);
expect(noErrors.classList.contains('selected-no-errors')).toBe(false);
});
test('two errors button can not be selected if bit is already selected', async () => {
const bits = await screen.findAllByText('0');
const bitToClick = bits[4];
expect(bitToClick).toBeInTheDocument();
fireEvent.click(bitToClick);
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors.classList.contains('two-errors')).toBe(true);
fireEvent.click(twoErrors);
expect(twoErrors.classList.contains('two-errors')).toBe(true);
});

test('two errors button can not be selected if no errors button is already selected', async () => {
const noErrors = await screen.findByText('If no errors, click here');
expect(noErrors).toBeInTheDocument();
fireEvent.click(noErrors);
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors.classList.contains('two-errors')).toBe(true);
fireEvent.click(twoErrors);
expect(twoErrors.classList.contains('two-errors')).toBe(true);
});

test('no errors button can not be selected if two errors button is already selected', async () => {
const twoErrors = await screen.findByText('If two errors, click here');
expect(twoErrors).toBeInTheDocument();
fireEvent.click(twoErrors);
const noErrors = await screen.findByText('If no errors, click here');
expect(noErrors).toBeInTheDocument();
fireEvent.click(noErrors);
expect(noErrors.classList.contains('no-errors')).toBe(true);
expect(noErrors.classList.contains('selected-no-errors')).toBe(false);
});

test('submit button on the practice page', async () => {
const submitTest = await screen.findByText('Submit Response');
expect(submitTest).toBeInTheDocument();
});

test('correct answer displays correct to user', async () => {
const mockApi = new MockAdapter(axios);
mockApi.onPost(`${process.env.REACT_APP_BASE_API}/api/HammingCodes`).reply(200, correctAnswerResponse);
fireEvent.click(await screen.findByText('If no errors, click here'));
fireEvent.click(screen.getByText('Submit Response'));
expect(await screen.findByText('Correct!')).toBeInTheDocument();
});

test('incorrect no errors displays correct response', async () => {
const mockApi = new MockAdapter(axios);
mockApi.onPost(`${process.env.REACT_APP_BASE_API}/api/HammingCodes`).reply(200, incorrectResponseNoErrors);
fireEvent.click(await screen.findByText('If no errors, click here'));
fireEvent.click(screen.getByText('Submit Response'));
expect(await screen.findByText('Incorrect. In this case, there were no errors.')).toBeInTheDocument();
});

test('incorrect two errors displays correct response', async () => {
const mockApi = new MockAdapter(axios);
mockApi.onPost(`${process.env.REACT_APP_BASE_API}/api/HammingCodes`).reply(200, incorrectResponseTwoErrors);
fireEvent.click(await screen.findByText('If no errors, click here'));
fireEvent.click(screen.getByText('Submit Response'));
expect(await screen.findByText('Incorrect. In this case, there were two errors.')).toBeInTheDocument();
});

test('submit button on the practice page', async () => {
testSetupAndRender();
const submitTest = await screen.findByText('Submit Response');
expect(submitTest).toBeInTheDocument();
test('incorrect two errors displays correct response', async () => {
const mockApi = new MockAdapter(axios);
mockApi.onPost(`${process.env.REACT_APP_BASE_API}/api/HammingCodes`).reply(200, incorrectResponseBitFlipped);
fireEvent.click(await screen.findByText('If no errors, click here'));
fireEvent.click(screen.getByText('Submit Response'));
expect(await screen.findByText('Incorrect. In this case, bit 4 was flipped.')).toBeInTheDocument();
});
});

0 comments on commit 1786244

Please sign in to comment.