|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import Counter from './Counter'; |
| 4 | +import "@testing-library/jest-dom/extend-expect" |
| 5 | + |
| 6 | +let getByTestId |
| 7 | +beforeEach(()=>{ |
| 8 | + const component = render(<Counter />) |
| 9 | + getByTestId = component.getByTestId |
| 10 | +}) |
| 11 | + |
| 12 | + |
| 13 | +test("header renders with correct test", ()=> { |
| 14 | + |
| 15 | + const headerElement = getByTestId("header"); |
| 16 | + |
| 17 | + expect(headerElement.textContent).toBe("My Counter"); |
| 18 | +}); |
| 19 | + |
| 20 | + |
| 21 | +test("counter element initially shows 0",()=>{ |
| 22 | + |
| 23 | + const counterElement = getByTestId("counter"); |
| 24 | + |
| 25 | + expect(counterElement.textContent).toBe("0"); |
| 26 | +}); |
| 27 | + |
| 28 | +// test("input element initially shows 1",()=>{ |
| 29 | + |
| 30 | +// const inputElement = getByTestId("input"); |
| 31 | + |
| 32 | +// expect(inputElement.value).toBe("1"); |
| 33 | +// }); |
| 34 | + |
| 35 | +test("add button renders +",()=>{ |
| 36 | + |
| 37 | + const addButton = getByTestId("add-button"); |
| 38 | + |
| 39 | + expect(addButton.textContent).toBe("+"); |
| 40 | +}); |
| 41 | +test("subtract button renders - ",()=>{ |
| 42 | + |
| 43 | + const subtractButton = getByTestId("subtract-button"); |
| 44 | + |
| 45 | + expect(subtractButton.textContent).toBe("-"); |
| 46 | +}); |
| 47 | + |
| 48 | +// test("input element value can be changed and used",()=>{ |
| 49 | + |
| 50 | +// const inputElement = getByTestId("input"); |
| 51 | +// fireEvent.change(inputElement,{target:{value: "5"}}) |
| 52 | +// expect(inputElement.value).toBe("5"); |
| 53 | +// }); |
| 54 | +test("input element value can be changed and used",()=>{ |
| 55 | + |
| 56 | + const inputElement = getByTestId("input"); |
| 57 | + const stepElement = getByTestId("step") |
| 58 | + |
| 59 | + expect(inputElement.value).toBe("1"); |
| 60 | + |
| 61 | + fireEvent.change(inputElement,{target:{value: "5"}}) |
| 62 | + expect(inputElement.value).toBe("5"); |
| 63 | + expect(stepElement.textContent).toBe("5"); |
| 64 | +}); |
| 65 | + |
| 66 | +test("clicking on + button adds 1 to counter initially",()=>{ |
| 67 | + |
| 68 | + const addButton = getByTestId("add-button"); |
| 69 | + const counterElement = getByTestId("counter"); |
| 70 | + expect(counterElement.textContent).toBe("0") |
| 71 | + fireEvent.click(addButton); |
| 72 | + |
| 73 | + expect(counterElement.textContent).toBe("1") |
| 74 | +}); |
| 75 | + |
| 76 | +test("changing input value then clicking add works",()=>{ |
| 77 | + |
| 78 | + const addButton = getByTestId("add-button"); |
| 79 | + const counterElement = getByTestId("counter"); |
| 80 | + const inputElement = getByTestId("input") |
| 81 | + |
| 82 | + fireEvent.change(inputElement,{target:{value: "5"}}) |
| 83 | + fireEvent.click(addButton); |
| 84 | + |
| 85 | + expect(counterElement.textContent).toBe("5") |
| 86 | + fireEvent.click(addButton); |
| 87 | + |
| 88 | + expect(counterElement.textContent).toBe("10") |
| 89 | +}); |
| 90 | + |
| 91 | +test("changing input value then clicking subtract works",()=>{ |
| 92 | + |
| 93 | + const subtractButton = getByTestId("subtract-button"); |
| 94 | + const counterElement = getByTestId("counter"); |
| 95 | + const inputElement = getByTestId("input") |
| 96 | + |
| 97 | + fireEvent.change(inputElement,{target:{value: "5"}}) |
| 98 | + fireEvent.click(subtractButton); |
| 99 | + |
| 100 | + expect(counterElement.textContent).toBe("-5") |
| 101 | +}); |
| 102 | + |
| 103 | +test("add then subtract should cancel back out to 0",()=>{ |
| 104 | + |
| 105 | + const addButton = getByTestId("add-button"); |
| 106 | + const subtractButton = getByTestId("subtract-button") |
| 107 | + const counterElement = getByTestId("counter"); |
| 108 | + const inputElement = getByTestId("input") |
| 109 | + |
| 110 | + fireEvent.change(inputElement,{target:{value: "5"}}) |
| 111 | + fireEvent.click(addButton); |
| 112 | + |
| 113 | + expect(counterElement.textContent).toBe("5") |
| 114 | + fireEvent.click(subtractButton); |
| 115 | + |
| 116 | + expect(counterElement.textContent).toBe("0") |
| 117 | +}); |
| 118 | + |
| 119 | +test("counter changes to green at value over 100",()=>{ |
| 120 | + |
| 121 | + const counterElement = getByTestId("counter"); |
| 122 | + const inputElement = getByTestId("input"); |
| 123 | + const addButton = getByTestId("add-button") |
| 124 | + fireEvent.change(inputElement,{target:{value: "50"}}) |
| 125 | + fireEvent.click(addButton); |
| 126 | + fireEvent.click(addButton); |
| 127 | + |
| 128 | + |
| 129 | + expect(counterElement.className).toContain("green") |
| 130 | +}); |
| 131 | + |
| 132 | +test("counter changes to red at negative values",()=>{ |
| 133 | + |
| 134 | + const counterElement = getByTestId("counter"); |
| 135 | + const inputElement = getByTestId("input"); |
| 136 | + const subtractButton = getByTestId("subtract-button") |
| 137 | + fireEvent.change(inputElement,{target:{value: "50"}}) |
| 138 | + fireEvent.click(subtractButton); |
| 139 | + fireEvent.click(subtractButton); |
| 140 | + |
| 141 | + |
| 142 | + expect(counterElement.className).toContain("red") |
| 143 | +}); |
0 commit comments