Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow consuming apps to use mirage factories/models/serializers #142

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
},
// mirage files
{
files: ['tests/dummy/mirage/**'],
files: ['tests/dummy/mirage/**', 'mirage-support/**'],
rules: {
'ember/avoid-leaking-state-in-ember-objects': 'off'
}
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ this.get('store').queryRecord('github-blob', { repo: 'jimmay5469/old-hash', sha:
this.get('store').queryRecord('github-tree', { repo: 'jimmay5469/old-hash', sha: '47c5438403ca875f170db2aa07d1bfa3689406e3' });
```

## Testing with Mirage

This addon uses [ember-cli-mirage](http://www.ember-cli-mirage.com/) in its tests. It is often beneficial for consuming apps to be able to re-use the factories and models defined in mirage, so if you would like to use these in your tests you can add the `mirage-support` object to your `ember-cli-build.js` file:

```
module.exports = function(defaults) {
let app = new EmberApp(defaults, {
...
'mirage-support': {
includeAll: true
}
...
});

return app.toTree();
};
```


As long as `ember-cli-mirage` is not disabled, the files in this addon's `mirage-support` directory will be merged with the consuming app's namespace, and be made available to that mirage context.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it's better to link back to mirage docs for this, if it's well-explained there? Otherwise we have to keep this in sync with the source package if there are changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is not a first-class thing that is supported by mirage so there is nothing over there to link to. I largely took this implementation from another addon that wanted to do the same thing for its mirage stubs. This is basically manually putting the mirage files into the build tree so consuming apps can access them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. Seems fine to leave it here for now. As extra credit, if you feel like it, it might be worth filing a feature request with mirage to support this internally so everyone can benefit from it.

The `'mirage-support'` key has 3 options:

Key | Type | Description
--- | --- | ---
`includeAll` | `Boolean` | If `true`, includes the full `mirage-support` tree, i.e. no-brainer just use it all.
`exclude` | _{Array of `GlobStrings,RegExps,Functions`}_ | This value gets passed directly to `broccoli-funnel`, *only* if `includeAll` is specified. Allows for excluding certain files from import.
`include` | _{Array of `GlobStrings,RegExps,Functions`}_ | Passed dirctly to `broccoli-funnel`. Allows to pick only certain files to be imported into app namespace.


## Contributing

### Installation
Expand Down
53 changes: 52 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
/* eslint-env node */
'use strict';

const path = require('path');
const MergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');

module.exports = {
name: 'ember-data-github'
name: 'ember-data-github',

included(app) {
this.addonConfig = this.app.project.config(app.env)['ember-cli-mirage'] || {};
this.mirageSupportDirectory = path.join(this.root, 'mirage-support');

this._super.included.apply(this, arguments);
},

treeForApp(appTree) {
var trees = [appTree];

if (this._shouldIncludeMirageFiles()) {
let mirageFolderName = 'mirage-support';
let mirageOptions = this.app.options[mirageFolderName];
let isDummyApp = this.app.name === 'dummy';

if (isDummyApp || mirageOptions) {
if (isDummyApp || mirageOptions.includeAll) {
trees.push(new Funnel(this.mirageSupportDirectory, {
destDir: 'mirage',
exclude: mirageOptions && mirageOptions.exclude || []
}));
} else if (mirageOptions.include) {
trees.push(new Funnel(this.mirageSupportDirectory, {
destDir: 'mirage',
include: mirageOptions.include
}));
}
}
}

return new MergeTrees(trees, {
overwrite: true
});
},

_shouldIncludeMirageFiles() {
if (process.env.EMBER_CLI_FASTBOOT) {
return false;
}

let enabledInProd = this.app.env === 'production' && this.addonConfig.enabled,
explicitExcludeFiles = this.addonConfig.excludeFilesFromBuild;

return enabledInProd || (this.app.env !== 'production' && explicitExcludeFiles !== true);//eslint-disable-line
}
};
File renamed without changes.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"changelog": "lerna-changelog"
},
"dependencies": {
"broccoli-funnel": "^2.0.1",
"broccoli-merge-trees": "^2.0.0",
"ember-cli-babel": "^6.9.0"
},
"devDependencies": {
Expand Down