Skip to content

Commit

Permalink
[Docs/Tests] use .have.lengthOf matcher over deprecated .length m…
Browse files Browse the repository at this point in the history
…atcher
  • Loading branch information
ljharb committed Aug 20, 2018
1 parent 0f0f052 commit 3fb4594
Show file tree
Hide file tree
Showing 34 changed files with 82 additions and 82 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ import Foo from './Foo';
describe('<MyComponent />', () => {
it('renders three <Foo /> components', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.find(Foo)).to.have.length(3);
expect(wrapper.find(Foo)).to.have.lengthOf(3);
});

it('renders an `.icon-star`', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.icon-star')).to.have.length(1);
expect(wrapper.find('.icon-star')).to.have.lengthOf(1);
});

it('renders children when passed in', () => {
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/children.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ selector can be provided and it will filter the children by this selector

```jsx
const wrapper = mount(<ToDoList items={items} />);
expect(wrapper.find('ul').children()).to.have.length(items.length);
expect(wrapper.find('ul').children()).to.have.lengthOf(items.length);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/closest.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Note: can only be called on a wrapper of a single node.

```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find(Foo).closest('.bar')).to.have.length(1);
expect(wrapper.find(Foo).closest('.bar')).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
24 changes: 12 additions & 12 deletions docs/api/ReactWrapper/detach.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const wrapper = mount(<Bar />, { attachTo: document.body });
const hydratedWrapper = mount(<Bar />, { hydrateIn: document.body });

// we can see that the component is rendered into the document
expect(wrapper.find('.in-bar')).to.have.length(1);
expect(document.body.childNodes).to.have.length(1);
expect(wrapper.find('.in-bar')).to.have.lengthOf(1);
expect(document.body.childNodes).to.have.lengthOf(1);

// detach it to clean up after yourself
wrapper.detach();

// now we can see that
expect(document.body.childNodes).to.have.length(0);
expect(document.body.childNodes).to.have.lengthOf(0);
```

Similarly, if you want to create some one-off elements for your test to mount into:
Expand All @@ -43,29 +43,29 @@ const div = global.document.createElement('div');
global.document.body.appendChild(div);

// div is empty. body has the div attached.
expect(document.body.childNodes).to.have.length(1);
expect(div.childNodes).to.have.length(0);
expect(document.body.childNodes).to.have.lengthOf(1);
expect(div.childNodes).to.have.lengthOf(0);

// mount a component passing div into the `attachTo` option
const wrapper = mount(<Foo />, { attachTo: div });
// or, mount a component passing div into the `hydrateIn` option
const hydratedWrapper = mount(<Foo />, { hydrateIn: div });

// we can see now the component is rendered into the document
expect(wrapper.find('.in-foo')).to.have.length(1);
expect(document.body.childNodes).to.have.length(1);
expect(div.childNodes).to.have.length(1);
expect(wrapper.find('.in-foo')).to.have.lengthOf(1);
expect(document.body.childNodes).to.have.lengthOf(1);
expect(div.childNodes).to.have.lengthOf(1);

// call detach to clean up
wrapper.detach();

// div is now empty, but still attached to the document
expect(document.body.childNodes).to.have.length(1);
expect(div.childNodes).to.have.length(0);
expect(document.body.childNodes).to.have.lengthOf(1);
expect(div.childNodes).to.have.lengthOf(0);

// remove div if you want
global.document.body.removeChild(div);

expect(document.body.childNodes).to.have.length(0);
expect(div.childNodes).to.have.length(0);
expect(document.body.childNodes).to.have.lengthOf(0);
expect(div.childNodes).to.have.lengthOf(0);
```
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Returns a new wrapper with only the nodes of the current wrapper that match the

```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.foo').filter('.bar')).to.have.length(1);
expect(wrapper.find('.foo').filter('.bar')).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/filterWhere.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ provided predicate function, return true.
```jsx
const wrapper = mount(<MyComponent />);
const complexComponents = wrapper.find('.foo').filterWhere(n => typeof n.type() !== 'string');
expect(complexComponents).to.have.length(4);
expect(complexComponents).to.have.lengthOf(4);
```


Expand Down
16 changes: 8 additions & 8 deletions docs/api/ReactWrapper/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ Finds every node in the render tree of the current wrapper that matches the prov
CSS Selectors:
```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.foo')).to.have.length(1);
expect(wrapper.find('.bar')).to.have.length(3);
expect(wrapper.find('.foo')).to.have.lengthOf(1);
expect(wrapper.find('.bar')).to.have.lengthOf(3);

// compound selector
expect(wrapper.find('div.some-class')).to.have.length(3);
expect(wrapper.find('div.some-class')).to.have.lengthOf(3);

// CSS id selector
expect(wrapper.find('#foo')).to.have.length(1);
expect(wrapper.find('#foo')).to.have.lengthOf(1);

// property selector
expect(wrapper.find('[htmlFor="checkbox"]')).to.have.length(1);
expect(wrapper.find('[htmlFor="checkbox"]')).to.have.lengthOf(1);
```

Component Constructors:
```jsx
import Foo from '../components/Foo';

const wrapper = mount(<MyComponent />);
expect(wrapper.find(Foo)).to.have.length(1);
expect(wrapper.find(Foo)).to.have.lengthOf(1);
```

Component Display Name:
```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find('Foo')).to.have.length(1);
expect(wrapper.find('Foo')).to.have.lengthOf(1);
```

Object Property Selector:
```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find({ prop: 'value' })).to.have.length(1);
expect(wrapper.find({ prop: 'value' })).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/findWhere.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nodes.
```jsx
const wrapper = mount(<MyComponent />);
const complexComponents = wrapper.findWhere(n => typeof n.type() !== 'string');
expect(complexComponents).to.have.length(8);
expect(complexComponents).to.have.lengthOf(8);
```


Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/hostNodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Returns a new wrapper with only host nodes.

```jsx
const wrapper = mount(<div><MyComponent className="foo" /><div className="foo" /></div>);
expect(wrapper.find('.foo').hostNodes()).to.have.length(1);
expect(wrapper.find('.foo').hostNodes()).to.have.lengthOf(1);
```
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/not.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This method is effectively the negation or inverse of [`filter`](filter.md).

```jsx
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.foo').not('.bar')).to.have.length(1);
expect(wrapper.find('.foo').not('.bar')).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/parents.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Note: can only be called on a wrapper of a single node.

```jsx
const wrapper = mount(<ToDoList />);
expect(wrapper.find('ul').parents()).to.have.length(2);
expect(wrapper.find('ul').parents()).to.have.lengthOf(2);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ function Bar() {

```jsx
const wrapper = mount(<Bar />);
expect(wrapper.find(Foo).render().find('.in-foo')).to.have.length(1);
expect(wrapper.find(Foo).render().find('.in-foo')).to.have.lengthOf(1);
```
8 changes: 4 additions & 4 deletions docs/api/ReactWrapper/setProps.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ Foo.propTypes = {
```
```jsx
const wrapper = mount(<Foo name="foo" />);
expect(wrapper.find('.foo')).to.have.length(1);
expect(wrapper.find('.bar')).to.have.length(0);
expect(wrapper.find('.foo')).to.have.lengthOf(1);
expect(wrapper.find('.bar')).to.have.lengthOf(0);
wrapper.setProps({ name: 'bar' });
expect(wrapper.find('.foo')).to.have.length(0);
expect(wrapper.find('.bar')).to.have.length(1);
expect(wrapper.find('.foo')).to.have.lengthOf(0);
expect(wrapper.find('.bar')).to.have.lengthOf(1);
```

```jsx
Expand Down
8 changes: 4 additions & 4 deletions docs/api/ReactWrapper/setState.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class Foo extends React.Component {
```
```jsx
const wrapper = mount(<Foo />);
expect(wrapper.find('.foo')).to.have.length(1);
expect(wrapper.find('.bar')).to.have.length(0);
expect(wrapper.find('.foo')).to.have.lengthOf(1);
expect(wrapper.find('.bar')).to.have.lengthOf(0);
wrapper.setState({ name: 'bar' });
expect(wrapper.find('.foo')).to.have.length(0);
expect(wrapper.find('.bar')).to.have.length(1);
expect(wrapper.find('.foo')).to.have.lengthOf(0);
expect(wrapper.find('.bar')).to.have.lengthOf(1);
```


Expand Down
4 changes: 2 additions & 2 deletions docs/api/ReactWrapper/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const wrapper = mount((
<div className="foo baz" />
</div>
));
expect(wrapper.find('.foo').slice(1)).to.have.length(2);
expect(wrapper.find('.foo').slice(1)).to.have.lengthOf(2);
expect(wrapper.find('.foo').slice(1).at(0).hasClass('bar')).to.equal(true);
expect(wrapper.find('.foo').slice(1).at(1).hasClass('baz')).to.equal(true);
```
Expand All @@ -39,6 +39,6 @@ const wrapper = mount((
<div className="foo baz" />
</div>
));
expect(wrapper.find('.foo').slice(1, 2)).to.have.length(1);
expect(wrapper.find('.foo').slice(1, 2)).to.have.lengthOf(1);
expect(wrapper.find('.foo').slice(1, 2).at(0).hasClass('bar')).to.equal(true);
```
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/children.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ selector can be provided and it will filter the children by this selector

```jsx
const wrapper = shallow(<ToDoList items={items} />);
expect(wrapper.find('ul').children()).to.have.length(items.length);
expect(wrapper.find('ul').children()).to.have.lengthOf(items.length);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/closest.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Note: can only be called on a wrapper of a single node.

```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find(Foo).closest('.bar')).to.have.length(1);
expect(wrapper.find(Foo).closest('.bar')).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
6 changes: 3 additions & 3 deletions docs/api/ShallowWrapper/dive.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Foo() {

```jsx
const wrapper = shallow(<Foo />);
expect(wrapper.find('.in-bar')).to.have.length(0);
expect(wrapper.find(Bar)).to.have.length(1);
expect(wrapper.find(Bar).dive().find('.in-bar')).to.have.length(1);
expect(wrapper.find('.in-bar')).to.have.lengthOf(0);
expect(wrapper.find(Bar)).to.have.lengthOf(1);
expect(wrapper.find(Bar).dive().find('.in-bar')).to.have.lengthOf(1);
```
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Returns a new wrapper with only the nodes of the current wrapper that match the

```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.foo').filter('.bar')).to.have.length(1);
expect(wrapper.find('.foo').filter('.bar')).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/filterWhere.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ provided predicate function, return true.
```jsx
const wrapper = shallow(<MyComponent />);
const complexFoo = wrapper.find('.foo').filterWhere(n => typeof n.type() !== 'string');
expect(complexFoo).to.have.length(4);
expect(complexFoo).to.have.lengthOf(4);
```


Expand Down
14 changes: 7 additions & 7 deletions docs/api/ShallowWrapper/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ Finds every node in the render tree of the current wrapper that matches the prov
CSS Selectors:
```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.foo')).to.have.length(1);
expect(wrapper.find('.bar')).to.have.length(3);
expect(wrapper.find('.foo')).to.have.lengthOf(1);
expect(wrapper.find('.bar')).to.have.lengthOf(3);

// compound selector
expect(wrapper.find('div.some-class')).to.have.length(3);
expect(wrapper.find('div.some-class')).to.have.lengthOf(3);

// CSS id selector
expect(wrapper.find('#foo')).to.have.length(1);
expect(wrapper.find('#foo')).to.have.lengthOf(1);
```

Component Constructors:
```jsx
import Foo from '../components/Foo';

const wrapper = shallow(<MyComponent />);
expect(wrapper.find(Foo)).to.have.length(1);
expect(wrapper.find(Foo)).to.have.lengthOf(1);
```

Component Display Name:
```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('Foo')).to.have.length(1);
expect(wrapper.find('Foo')).to.have.lengthOf(1);
```

Object Property Selector:
```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find({ prop: 'value' })).to.have.length(1);
expect(wrapper.find({ prop: 'value' })).to.have.lengthOf(1);
```


Expand Down
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/findWhere.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nodes.
```jsx
const wrapper = shallow(<MyComponent />);
const complexComponents = wrapper.findWhere(n => n.type() !== 'string');
expect(complexComponents).to.have.length(8);
expect(complexComponents).to.have.lengthOf(8);
```


Expand Down
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/hostNodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Returns a new wrapper with only host nodes.

```jsx
const wrapper = shallow(<div><MyComponent className="foo" /><div className="foo" /></div>);
expect(wrapper.find('.foo').hostNodes()).to.have.length(1);
expect(wrapper.find('.foo').hostNodes()).to.have.lengthOf(1);
```
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/not.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This method is effectively the negation or inverse of [`filter`](filter.md).

```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.foo').not('.bar')).to.have.length(1);
expect(wrapper.find('.foo').not('.bar')).to.have.lengthOf(1);
```

#### Related Methods
Expand Down
2 changes: 1 addition & 1 deletion docs/api/ShallowWrapper/parents.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Note: can only be called on a wrapper of a single node.

```jsx
const wrapper = shallow(<ToDoList />);
expect(wrapper.find('ul').parents()).to.have.length(2);
expect(wrapper.find('ul').parents()).to.have.lengthOf(2);
```

#### Related Methods
Expand Down
4 changes: 2 additions & 2 deletions docs/api/ShallowWrapper/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ function Bar() {

```jsx
const wrapper = shallow(<Bar />);
expect(wrapper.find('.in-foo')).to.have.length(0);
expect(wrapper.find(Foo).render().find('.in-foo')).to.have.length(1);
expect(wrapper.find('.in-foo')).to.have.lengthOf(0);
expect(wrapper.find(Foo).render().find('.in-foo')).to.have.lengthOf(1);
```
8 changes: 4 additions & 4 deletions docs/api/ShallowWrapper/setProps.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ Foo.propTypes = {
```
```jsx
const wrapper = shallow(<Foo name="foo" />);
expect(wrapper.find('.foo')).to.have.length(1);
expect(wrapper.find('.bar')).to.have.length(0);
expect(wrapper.find('.foo')).to.have.lengthOf(1);
expect(wrapper.find('.bar')).to.have.lengthOf(0);
wrapper.setProps({ name: 'bar' });
expect(wrapper.find('.foo')).to.have.length(0);
expect(wrapper.find('.bar')).to.have.length(1);
expect(wrapper.find('.foo')).to.have.lengthOf(0);
expect(wrapper.find('.bar')).to.have.lengthOf(1);
```

```jsx
Expand Down

0 comments on commit 3fb4594

Please sign in to comment.