forked from miragejs/ember-cli-mirage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documention clarifying use of scenarios (#1427)
- Loading branch information
1 parent
0ead4fa
commit 4ffafc7
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
tests/dummy/app/pods/docs/advanced/switching-between-scenarios/template.md
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,66 @@ | ||
# Switching between scenarios | ||
|
||
You can use the scenarios directory to organize your scenario files. Scenarios | ||
might be useful if you need to conduct [user acceptance testing](https://en.wikipedia.org/wiki/Acceptance_testing#User_acceptance_testing) | ||
of your application using Mirage data. For example, you might test whether a user | ||
can complete a set of tasks specific to unique circumstances defined in a scenario. | ||
|
||
Currently, scenarios are not a first-class API: Mirage only uses the default scenario | ||
in your development environment. | ||
|
||
That said, you can still configure your default scenario file to honor options passed | ||
to the runtime environment. For example, if you created a scenario file called `some-scenario.js` | ||
and wanted to load that scenario based on an environment variable called `MIRAGE_SCENARIO`, you | ||
could add the following to `scenarios/default.js`: | ||
|
||
```js | ||
import { camelize } from '@ember/string'; | ||
import ENV from '../../config/environment'; | ||
|
||
export default function(server) { | ||
const { MIRAGE_SCENARIO = '' } = ENV; | ||
const scenario = server._config.scenarios[camelize(MIRAGE_SCENARIO)]; | ||
|
||
if (scenario) { | ||
scenario(server); | ||
} | ||
|
||
// plus whatever default scenario code you want | ||
} | ||
|
||
``` | ||
|
||
You would also need to pull the environment variable into your `environment/config.js`: | ||
|
||
```js | ||
const { MIRAGE_SCENARIO } = process.env.MIRAGE_SCENARIO; | ||
|
||
module.exports = function(environment) { | ||
const ENV = { | ||
// ...other stuff | ||
|
||
MIRAGE_SCENARIO, | ||
|
||
// ...other stuff | ||
}; | ||
|
||
return ENV; | ||
} | ||
``` | ||
|
||
Finally, you could switch between scenario files on the command line: | ||
|
||
```bash | ||
MIRAGE_SCENARIO=some-scenario ember s | ||
``` | ||
|
||
Like other Mirage objects, all scenario files must export a function like so: | ||
|
||
```js | ||
export default function(server) { | ||
// server.createList('post', 15); | ||
} | ||
``` | ||
|
||
Note that the `server` parameter is not provided by default. | ||
|
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