This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mocking defaultProps method example (#359)
- Loading branch information
Showing
13 changed files
with
233 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import { fetchIngredients as defaultFetchIngredients } from './services' | ||
|
||
export default function PizzaProps({ fetchIngredients }) { | ||
const [ingredients, setIngredients] = React.useState([]) | ||
|
||
const handleCook = () => { | ||
fetchIngredients().then(response => { | ||
setIngredients(response.args.ingredients) | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<h3>Pizza</h3> | ||
<button onClick={handleCook}>Cook</button> | ||
{ingredients.length > 0 && ( | ||
<ul> | ||
{ingredients.map(ingredient => ( | ||
<li key={ingredient}>{ingredient}</li> | ||
))} | ||
</ul> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
PizzaProps.defaultProps = { | ||
fetchIngredients: defaultFetchIngredients, | ||
} |
21 changes: 21 additions & 0 deletions
21
cypress/component/advanced/mocking-imports/PizzaProps.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
import PizzaProps from './PizzaProps' | ||
import { mount } from 'cypress-react-unit-test' | ||
|
||
const ingredients = ['bacon', 'tomato', 'mozzarella', 'pineapples'] | ||
|
||
describe('PizzaProps', () => { | ||
it('mocks method in the default props', () => { | ||
cy.stub(PizzaProps.defaultProps, 'fetchIngredients') | ||
.resolves({ args: { ingredients } }) | ||
.as('fetchMock') | ||
mount(<PizzaProps />) | ||
cy.contains('button', /cook/i).click() | ||
|
||
for (const ingredient of ingredients) { | ||
cy.contains(ingredient) | ||
} | ||
|
||
cy.get('@fetchMock').should('have.been.calledOnce') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
import { fetchIngredients as defaultFetchIngredients } from './services' | ||
|
||
export default function RemotePizza() { | ||
const [ingredients, setIngredients] = React.useState([]) | ||
|
||
const handleCook = () => { | ||
defaultFetchIngredients().then(response => { | ||
setIngredients(response.args.ingredients) | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<h3>Pizza</h3> | ||
<button onClick={handleCook}>Cook</button> | ||
{ingredients.length > 0 && ( | ||
<ul> | ||
{ingredients.map(ingredient => ( | ||
<li key={ingredient}>{ingredient}</li> | ||
))} | ||
</ul> | ||
)} | ||
</> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
cypress/component/advanced/mocking-imports/RemotePizza.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react' | ||
import RemotePizza from './RemotePizza' | ||
import { mount } from 'cypress-react-unit-test' | ||
// prepare for import mocking | ||
import * as services from './services' | ||
|
||
const ingredients = ['bacon', 'tomato', 'mozzarella', 'pineapples'] | ||
|
||
describe('RemotePizza', () => { | ||
it('mocks named import from services', () => { | ||
cy.stub(services, 'fetchIngredients') | ||
.resolves({ args: { ingredients } }) | ||
.as('fetchMock') | ||
mount(<RemotePizza />) | ||
cy.contains('button', /cook/i).click() | ||
|
||
for (const ingredient of ingredients) { | ||
cy.contains(ingredient) | ||
} | ||
|
||
cy.get('@fetchMock').should('have.been.calledOnce') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const fetchIngredients = () => | ||
fetch( | ||
'https://httpbin.org/anything?ingredients=bacon&ingredients=mozzarella&ingredients=pineapples', | ||
).then(r => r.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react' | ||
import RemotePizza from './RemotePizza' | ||
import { mount } from 'cypress-react-unit-test' | ||
|
||
const ingredients = ['bacon', 'tomato', 'mozzarella', 'pineapples'] | ||
|
||
describe('RemotePizza', () => { | ||
it('download ingredients from internets (network mock)', () => { | ||
cy.server() | ||
cy.route('https://httpbin.org/anything*', { args: { ingredients } }).as( | ||
'pizza', | ||
) | ||
|
||
mount(<RemotePizza />) | ||
cy.contains('button', /cook/i).click() | ||
cy.wait('@pizza') // make sure the network stub was used | ||
|
||
for (const ingredient of ingredients) { | ||
cy.contains(ingredient) | ||
} | ||
}) | ||
|
||
it('stubs via prop (di)', () => { | ||
const fetchIngredients = cy.stub().resolves({ args: { ingredients } }) | ||
mount(<RemotePizza fetchIngredients={fetchIngredients} />) | ||
cy.contains('button', /cook/i).click() | ||
|
||
for (const ingredient of ingredients) { | ||
cy.contains(ingredient) | ||
} | ||
}) | ||
|
||
it('mocks default props method', () => { | ||
cy.stub(RemotePizza.defaultProps, 'fetchIngredients').resolves({ | ||
args: { ingredients }, | ||
}) | ||
mount(<RemotePizza />) | ||
cy.contains('button', /cook/i).click() | ||
|
||
for (const ingredient of ingredients) { | ||
cy.contains(ingredient) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import { fetchIngredients as defaultFetchIngredients } from './services' | ||
|
||
export default function RemotePizza({ fetchIngredients }) { | ||
const [ingredients, setIngredients] = React.useState([]) | ||
|
||
const handleCook = () => { | ||
fetchIngredients().then(response => { | ||
setIngredients(response.args.ingredients) | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<h3>Pizza</h3> | ||
<button onClick={handleCook}>Cook</button> | ||
{ingredients.length > 0 && ( | ||
<ul> | ||
{ingredients.map(ingredient => ( | ||
<li key={ingredient}>{ingredient}</li> | ||
))} | ||
</ul> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
RemotePizza.defaultProps = { | ||
fetchIngredients: defaultFetchIngredients, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const fetchIngredients = () => | ||
fetch( | ||
'https://httpbin.org/anything?ingredients=bacon&ingredients=mozzarella&ingredients=pineapples', | ||
).then(r => r.json()) |