// jest-dom adds custom jest matchers for asserting on DOM nodes. // allows you to do things like: // expect(element).toHaveTextContent(/react/i) // learn more: https://github.com/testing-library/jest-dom import'@testing-library/jest-dom/extend-expect';
exportinterface Props { name: string, enthusiasmLevel?: number }
// Method I: Define React Component by function functionHello({ name, enthusiasmLevel = 1}: Props) { if (enthusiasmLevel <= 0) { thrownewError('You could be a little more enthusiastic. :D'); }
import * as React from'react'; import * as enzyme from'enzyme'; import Hello from'./Hello';
it('renders the correct text when no enthusiasm level is given', () => { const hello = enzyme.shallow(<Hello name='Daniel' />); expect(hello.find(".greeting").text()).toEqual('Hello Daniel!') });
it('renders the correct text with an explicit enthusiasm of 1', () => { const hello = enzyme.shallow(<Hello name='Daniel' enthusiasmLevel={1}/>); expect(hello.find(".greeting").text()).toEqual('Hello Daniel!') });
it('renders the correct text with an explicit enthusiasm level of 5', () => { const hello = enzyme.shallow(<Hello name='Daniel' enthusiasmLevel={5} />); expect(hello.find(".greeting").text()).toEqual('Hello Daniel!!!!!'); });
it('throws when the enthusiasm level is 0', () => { expect(() => { enzyme.shallow(<Hello name='Daniel' enthusiasmLevel={0} />); }).toThrow(); });
it('throws when the enthusiasm level is negative', () => { expect(() => { enzyme.shallow(<Hello name='Daniel' enthusiasmLevel={-1} />); }).toThrow(); });