Skip to content

Commit 6776845

Browse files
refactor
1 parent 18e1812 commit 6776845

File tree

7 files changed

+57
-84
lines changed

7 files changed

+57
-84
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ cy.getReact('MyTextInput', { field: { name: 'email' } })
232232
cy.getReact('MyTextInput', { field: { name: 'email' } }).getProps();
233233
```
234234

235-
![get-props](./screenshots/get-props.png)
235+
![get-props](./docs/get-props.png)
236236

237237
### Get current state
238238

@@ -267,7 +267,7 @@ Checkout sample tests [here](./cypress/integration)
267267

268268
Use [React Dev Tools](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en) plugin to easily identify the react component, props and state. Have a look in the below demonstration, how I have used the tool to write the sample test cases.
269269

270-
![react-dev-tools](./screenshots/cy-react-dev-tool.gif)
270+
![react-dev-tools](./docs/cy-react-dev-tool.gif)
271271

272272
## Sample Example Project
273273

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
describe('It should validate cypress react selector', () => {
2+
before(() => {
3+
cy.visit('https://ahfarmer.github.io/calculator/');
4+
cy.waitForReact();
5+
});
6+
7+
it('it should validate react selection with component only', () => {
8+
cy.react('t').should('have.length', '22');
9+
});
10+
11+
it('it should validate react selection component and props', () => {
12+
cy.react('t', { name: '5' }).should('have.text', '5');
13+
});
14+
15+
it('it should validate react selection with wildcard', () => {
16+
cy.react('*', { name: '9' }).should('have.text', '9');
17+
});
18+
19+
it('it should validate react selection with cypress find command', () => {
20+
cy.react('t', { name: '5' }).find('button').should('have.text', '5');
21+
});
22+
23+
it('should calculate 7 * 6', () => {
24+
cy.react('t', { name: '7' }).click();
25+
cy.react('t', { name: 'x' }).click();
26+
cy.react('t', { name: '6' }).click();
27+
cy.react('t', { name: '=' }).click();
28+
29+
cy.get('.component-display').should('have.text', '42');
30+
});
31+
32+
it('it should validate getProps with specific prop name', () => {
33+
cy.getReact('t', { name: '5' }).getProps('name').should('eq', '5');
34+
});
35+
36+
it('it should validate getProps with no specific prop name', () => {
37+
cy.getReact('t', { name: '5' })
38+
.getProps()
39+
.then((props) => {
40+
expect(JSON.stringify(props)).to.contain('5');
41+
});
42+
});
43+
44+
it('it should validate react chaining', () => {
45+
cy.react('t', { name: '5' }).react('button').should('have.text', '5');
46+
});
47+
48+
it('it should validate chained react instances getProps with specific prop name', () => {
49+
cy.getReact('t')
50+
.getReact('t', { name: '5' })
51+
.getProps('name')
52+
.should('eq', '5');
53+
});
54+
});

cypress/integration/react-selector.spec.js

-60
This file was deleted.
File renamed without changes.
File renamed without changes.

index.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import { waitForReact } from './src/resqInjector';
2-
import {
3-
react,
4-
getReact,
5-
getProps,
6-
getCurrentState,
7-
setProps,
8-
} from './src/reactHandler';
2+
import { react, getReact, getProps, getCurrentState } from './src/reactHandler';
93

104
// add cypress custom commands
115
Cypress.Commands.add('waitForReact', waitForReact);
126
Cypress.Commands.add('react', { prevSubject: ['optional', 'element'] }, react);
137
Cypress.Commands.add('getReact', { prevSubject: 'optional' }, getReact);
148
Cypress.Commands.add('getProps', { prevSubject: true }, getProps);
159
Cypress.Commands.add('getCurrentState', { prevSubject: true }, getCurrentState);
16-
Cypress.Commands.add('setProps', { prevSubject: true }, setProps);

src/reactHandler.js

-14
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,3 @@ export const getCurrentState = (subject) => {
166166
);
167167
return cy.wrap(subject[0].state);
168168
};
169-
170-
/**
171-
* get all props or specific props from react node
172-
* @param {*} subject
173-
* @param {*} propName
174-
*/
175-
export const setProps = (subject, propName) => {
176-
cy.log(`subject: ${subject[0].props.brand}`);
177-
subject[0].props.brand = 'pop';
178-
cy.log(`subject: ${subject[0].props.brand}`);
179-
if (!subject || !subject[0].props) {
180-
throw new Error('getProps() is a child command. Use with cy.getReact()');
181-
}
182-
};

0 commit comments

Comments
 (0)