Skip to content

Commit

Permalink
Update testing.mdx (#242)
Browse files Browse the repository at this point in the history
* Update testing.mdx

added documentation fot api testing with cypress

* Minor edits

---------

Co-authored-by: Jan C. Brammer <[email protected]>
  • Loading branch information
mehmood86 and JanCBrammer authored Aug 31, 2023
1 parent f38f646 commit 17575f8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/development/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,41 @@ cy.createDefaultUser('[email protected]', 'cu1').then((user) => {
});
```
### Example of testing an API endpoint with Cypress
You can also use Cypress to test APIs in a developer-friendly way.
You can conveniently access the API response data from the test runner.
When you click on the request step, Cypress will display the corresponding response data in the browser's console.
Consider the following example, where we are testing `Collection API (GET request)`.
```javascript
cy.request('/api/v1/collections/roots.json').as('roots');
cy.get('@roots').then(response => {
expect(response.status).to.eq(200);
const collectionData = response.body['collections'][0];
expect(collectionData.id).to.eq(3);
expect(collectionData.label).to.eq('Col1');
});
```
Here, were are sending a GET request to the `api/v1/collections/roots.json` endpoint and verify that its `response.status` is `200`.
The response body can contain objects, arrays, attributes, etc. This is how we can access a specific component from its body and perform checks on it.
```javascript
// whole object
const collectionData = response.body['collections'];

// single item from the object list
const collectionData = response.body['collections'][0];

// check length of object
expect(response.body['collections'].length).to.eq(0)

// check if returned fields are matching exactly what's expected
expect(collectionData.id).to.eq(3);
expect(collectionData.label).to.eq('Col1');
```
## General rules of thumb
- All public methods should be tested, every statement, conditional branch should be covered.
Expand Down

0 comments on commit 17575f8

Please sign in to comment.