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

update to react test library #1798

Merged
merged 9 commits into from
Dec 14, 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
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
// License: LGPL-3.0-or-later
import * as React from 'react';
import {shallow} from 'enzyme'
import toJson from 'enzyme-to-json'
import {render} from '@testing-library/react'
import LabeledFieldComponent from './LabeledFieldComponent'

describe('LabeledFieldComponent', () => {
test('In Error with Children', () => {
let result = shallow(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={true}
let result = render(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={true}
error={"errorMessage"}>
<hr/>
</LabeledFieldComponent>)
expect(toJson(result)).toMatchSnapshot()
expect(result.baseElement).toMatchSnapshot()
})

test('has error checked but no message so not really in error', () => {
let result = shallow(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={true} error={undefined}>
let result = render(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={true} error={undefined}>
<hr/>
</LabeledFieldComponent>)
expect(toJson(result)).toMatchSnapshot()
expect(result.baseElement).toMatchSnapshot()
})

test('no error', () => {
let result = shallow(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={false}>
let result = render(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={false}>
<hr/>
</LabeledFieldComponent>)
expect(toJson(result)).toMatchSnapshot()
expect(result.baseElement).toMatchSnapshot()
})

test('add extra classNames', () => {
let result = shallow(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={false}
let result = render(<LabeledFieldComponent inputId={"ID"} labelText={"Our Label"} inError={false}
className={"a_class another_class"}>
<hr/>
</LabeledFieldComponent>)
expect(toJson(result)).toMatchSnapshot()
expect(result.baseElement).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import * as React from 'react';
import Modal, {ModalProps} from './Modal'
import {shallow, mount, ReactWrapper} from "enzyme";
import toJson from "enzyme-to-json";
import {render, screen} from '@testing-library/react'
import { DefaultCloseButton } from './DefaultCloseButton';

describe('Modal', () => {
test('nothing displayed if inactive', () => {
let modal = shallow(<Modal childGenerator={() => <div/>}/>)
let modal = render(<Modal childGenerator={() => <div/>}/>)

expect(toJson(modal)).toMatchSnapshot()
expect(modal.baseElement.outerHTML).toMatchSnapshot()
})

describe('active modal displays', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
// License: LGPL-3.0-or-later
import * as React from 'react';
import ProgressableButton from './ProgressableButton'
import toJson from 'enzyme-to-json';
import {mount, shallow} from 'enzyme';
import {render, screen} from '@testing-library/react'
import userEvent from '@testing-library/user-event'

describe('ProgressableButton', () => {
test('Basic title button works', () => {
let output = shallow(
<ProgressableButton onClick={() => console.log('alert!')} buttonText={"nothing"} data-label="button"/>)
expect(toJson(output)).toMatchSnapshot()
test('Basic title button works', async () => {
const clicked = jest.fn()
let output = render(
<ProgressableButton onClick={clicked} buttonText={"nothing"} data-label="button"/>)

userEvent.click(screen.getByText("nothing"))
expect(clicked).toBeCalled();

expect(output.baseElement).toMatchSnapshot()


})

test('Progress means we change the title, dont disable and do turn on spinner', () => {
let output = mount(
<ProgressableButton onClick={() => console.log('alert!')}
const clicked = jest.fn()
let output = render(
<ProgressableButton onClick={clicked}
buttonText={"nothing"}
data-label="button"
buttonTextOnProgress={"onProgress"}
inProgress={true}
disableOnProgress={false}

/>)
expect(toJson(output)).toMatchSnapshot()
expect(output.baseElement).toMatchSnapshot()
})

test('Title is kept on progress if no titleOnProgress is set', () => {
let output = mount(
<ProgressableButton onClick={() => console.log('alert!')}
let output = render(
<ProgressableButton onClick={() => {}}
buttonText={"nothing"}
data-label="button"
inProgress={true}

/>)
expect(toJson(output)).toMatchSnapshot()
expect(output.baseElement).toMatchSnapshot()
})

test('Progress means we change the title, disable and do turn on spinner', () => {
let output = mount(
let output = render(
<ProgressableButton onClick={() => console.log('alert!')}
buttonText={"nothing"}
data-label="button"
Expand All @@ -45,12 +53,12 @@ describe('ProgressableButton', () => {
disableOnProgress={true}

/>)
expect(toJson(output)).toMatchSnapshot()
expect(output.baseElement).toMatchSnapshot()
})


test('Disabled manually set overrides whether we disable on progress when in progress', () => {
let output = mount(
let output = render(
<ProgressableButton onClick={() => console.log('alert!')}
buttonText={"nothing"}
data-label="button"
Expand All @@ -60,12 +68,12 @@ describe('ProgressableButton', () => {
disabled={true}

/>)
expect(toJson(output)).toMatchSnapshot()
expect(output.baseElement).toMatchSnapshot()
})


test('Disabled manually set overrides whether we disable on progress when NOT in progress', () => {
let output = mount(
let output = render(
<ProgressableButton onClick={() => console.log('alert!')}
buttonText={"nothing"}
data-label="button"
Expand All @@ -75,7 +83,7 @@ describe('ProgressableButton', () => {
disabled={true}

/>)
expect(toJson(output)).toMatchSnapshot()
expect(output.baseElement).toMatchSnapshot()
})


Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// License: LGPL-3.0-or-later
import * as React from 'react';
import ScreenReaderOnlyText from './ScreenReaderOnlyText';
import toJson from 'enzyme-to-json';
import { shallow } from 'enzyme';
import {render} from '@testing-library/react';

describe('ScreenReaderOnlyText', () => {
it('renders properly', () => {
expect.hasAssertions();
const text = shallow(<ScreenReaderOnlyText>Test</ScreenReaderOnlyText>);
const text = render(<ScreenReaderOnlyText>Test</ScreenReaderOnlyText>);
// eslint-disable-next-line jest/prefer-inline-snapshots
expect(toJson(text)).toMatchSnapshot();
expect(text.baseElement).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// License: LGPL-3.0-or-later
import * as React from 'react';
import {shallow} from 'enzyme'
import {render} from '@testing-library/react'
import StandardFieldComponent from './StandardFieldComponent'
import toJson from 'enzyme-to-json';

describe('StandardFieldComponent', () => {
test('works with no children', () => {
var field = shallow(<StandardFieldComponent inError={false} />)
var field = render(<StandardFieldComponent inError={false} />)


expect(toJson(field)).toMatchSnapshot()
expect(field.baseElement).toMatchSnapshot()
})
test('works with a child', () => {
var field = shallow(<StandardFieldComponent inError={false}><input/></StandardFieldComponent>);
var field = render(<StandardFieldComponent inError={false}><input/></StandardFieldComponent>);

expect(toJson(field)).toMatchSnapshot()
expect(field.baseElement).toMatchSnapshot()
})

test('sets error message properly', () => {
var field = shallow(<StandardFieldComponent inError={true} error={"Something more"}><input/></StandardFieldComponent>);
var field = render(<StandardFieldComponent inError={true} error={"Something more"}><input/></StandardFieldComponent>);

expect(toJson(field)).toMatchSnapshot()
expect(field.baseElement).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -1,74 +1,94 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`LabeledFieldComponent In Error with Children 1`] = `
<fieldset
className="form-group has-error"
>
<label
className="control-label"
htmlFor="ID"
>
Our Label
</label>
<StandardFieldComponent
error="errorMessage"
inError={true}
>
<hr />
</StandardFieldComponent>
</fieldset>
<body>
<div>
<fieldset
class="form-group has-error"
>
<label
class="control-label"
for="ID"
>
Our Label
</label>
<div>
<hr />
<div
class="help-block"
role="alert"
>
errorMessage
</div>

</div>
</fieldset>
</div>
</body>
`;

exports[`LabeledFieldComponent add extra classNames 1`] = `
<fieldset
className="a_class another_class form-group"
>
<label
className="control-label"
htmlFor="ID"
>
Our Label
</label>
<StandardFieldComponent
inError={false}
>
<hr />
</StandardFieldComponent>
</fieldset>
<body>
<div>
<fieldset
class="a_class another_class form-group"
>
<label
class="control-label"
for="ID"
>
Our Label
</label>
<div>
<hr />


</div>
</fieldset>
</div>
</body>
`;

exports[`LabeledFieldComponent has error checked but no message so not really in error 1`] = `
<fieldset
className="form-group"
>
<label
className="control-label"
htmlFor="ID"
>
Our Label
</label>
<StandardFieldComponent
inError={false}
>
<hr />
</StandardFieldComponent>
</fieldset>
<body>
<div>
<fieldset
class="form-group"
>
<label
class="control-label"
for="ID"
>
Our Label
</label>
<div>
<hr />


</div>
</fieldset>
</div>
</body>
`;

exports[`LabeledFieldComponent no error 1`] = `
<fieldset
className="form-group"
>
<label
className="control-label"
htmlFor="ID"
>
Our Label
</label>
<StandardFieldComponent
inError={false}
>
<hr />
</StandardFieldComponent>
</fieldset>
<body>
<div>
<fieldset
class="form-group"
>
<label
class="control-label"
for="ID"
>
Our Label
</label>
<div>
<hr />


</div>
</fieldset>
</div>
</body>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,4 @@ exports[`Modal active modal displays matches snapshot 1`] = `
</Modal>
`;

exports[`Modal nothing displayed if inactive 1`] = `""`;
exports[`Modal nothing displayed if inactive 1`] = `"<body><div></div></body>"`;
Loading
Loading