From de35f5c627e9e5d728a759fe57d8d22135c305db Mon Sep 17 00:00:00 2001 From: Brian Runnells Date: Mon, 12 Feb 2018 16:49:35 -0600 Subject: [PATCH] allow consuming apps to use mirage factories/models/serializers --- .eslintrc.js | 2 +- README.md | 29 ++++++++++ index.js | 53 ++++++++++++++++++- .../factories/github-blob.js | 0 .../factories/github-branch.js | 0 .../factories/github-commit.js | 0 .../factories/github-compare.js | 0 .../factories/github-file.js | 0 .../factories/github-organization.js | 0 .../factories/github-pull.js | 0 .../factories/github-release.js | 0 .../factories/github-repository.js | 0 .../factories/github-tree.js | 0 .../factories/github-user.js | 0 .../factories/tree.js | 0 .../models/github-branch.js | 0 .../models/github-commit.js | 0 .../models/github-compare.js | 0 .../models/github-file.js | 0 .../models/github-organization.js | 0 .../models/github-pull.js | 0 .../models/github-release.js | 0 .../models/github-repository.js | 0 .../models/github-user.js | 0 .../serializers/application.js | 0 .../serializers/github-compare.js | 0 .../serializers/github-pull.js | 0 .../serializers/github-release.js | 0 .../serializers/github-repository.js | 0 package.json | 2 + 30 files changed, 84 insertions(+), 2 deletions(-) rename {tests/dummy/mirage => mirage-support}/factories/github-blob.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-branch.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-commit.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-compare.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-file.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-organization.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-pull.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-release.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-repository.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-tree.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/github-user.js (100%) rename {tests/dummy/mirage => mirage-support}/factories/tree.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-branch.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-commit.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-compare.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-file.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-organization.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-pull.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-release.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-repository.js (100%) rename {tests/dummy/mirage => mirage-support}/models/github-user.js (100%) rename {tests/dummy/mirage => mirage-support}/serializers/application.js (100%) rename {tests/dummy/mirage => mirage-support}/serializers/github-compare.js (100%) rename {tests/dummy/mirage => mirage-support}/serializers/github-pull.js (100%) rename {tests/dummy/mirage => mirage-support}/serializers/github-release.js (100%) rename {tests/dummy/mirage => mirage-support}/serializers/github-repository.js (100%) diff --git a/.eslintrc.js b/.eslintrc.js index a0fcfd6..fd1c26e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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' } diff --git a/README.md b/README.md index 2aefff5..6316785 100644 --- a/README.md +++ b/README.md @@ -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. +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 diff --git a/index.js b/index.js index c31ebc9..c7eed41 100644 --- a/index.js +++ b/index.js @@ -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 + } }; diff --git a/tests/dummy/mirage/factories/github-blob.js b/mirage-support/factories/github-blob.js similarity index 100% rename from tests/dummy/mirage/factories/github-blob.js rename to mirage-support/factories/github-blob.js diff --git a/tests/dummy/mirage/factories/github-branch.js b/mirage-support/factories/github-branch.js similarity index 100% rename from tests/dummy/mirage/factories/github-branch.js rename to mirage-support/factories/github-branch.js diff --git a/tests/dummy/mirage/factories/github-commit.js b/mirage-support/factories/github-commit.js similarity index 100% rename from tests/dummy/mirage/factories/github-commit.js rename to mirage-support/factories/github-commit.js diff --git a/tests/dummy/mirage/factories/github-compare.js b/mirage-support/factories/github-compare.js similarity index 100% rename from tests/dummy/mirage/factories/github-compare.js rename to mirage-support/factories/github-compare.js diff --git a/tests/dummy/mirage/factories/github-file.js b/mirage-support/factories/github-file.js similarity index 100% rename from tests/dummy/mirage/factories/github-file.js rename to mirage-support/factories/github-file.js diff --git a/tests/dummy/mirage/factories/github-organization.js b/mirage-support/factories/github-organization.js similarity index 100% rename from tests/dummy/mirage/factories/github-organization.js rename to mirage-support/factories/github-organization.js diff --git a/tests/dummy/mirage/factories/github-pull.js b/mirage-support/factories/github-pull.js similarity index 100% rename from tests/dummy/mirage/factories/github-pull.js rename to mirage-support/factories/github-pull.js diff --git a/tests/dummy/mirage/factories/github-release.js b/mirage-support/factories/github-release.js similarity index 100% rename from tests/dummy/mirage/factories/github-release.js rename to mirage-support/factories/github-release.js diff --git a/tests/dummy/mirage/factories/github-repository.js b/mirage-support/factories/github-repository.js similarity index 100% rename from tests/dummy/mirage/factories/github-repository.js rename to mirage-support/factories/github-repository.js diff --git a/tests/dummy/mirage/factories/github-tree.js b/mirage-support/factories/github-tree.js similarity index 100% rename from tests/dummy/mirage/factories/github-tree.js rename to mirage-support/factories/github-tree.js diff --git a/tests/dummy/mirage/factories/github-user.js b/mirage-support/factories/github-user.js similarity index 100% rename from tests/dummy/mirage/factories/github-user.js rename to mirage-support/factories/github-user.js diff --git a/tests/dummy/mirage/factories/tree.js b/mirage-support/factories/tree.js similarity index 100% rename from tests/dummy/mirage/factories/tree.js rename to mirage-support/factories/tree.js diff --git a/tests/dummy/mirage/models/github-branch.js b/mirage-support/models/github-branch.js similarity index 100% rename from tests/dummy/mirage/models/github-branch.js rename to mirage-support/models/github-branch.js diff --git a/tests/dummy/mirage/models/github-commit.js b/mirage-support/models/github-commit.js similarity index 100% rename from tests/dummy/mirage/models/github-commit.js rename to mirage-support/models/github-commit.js diff --git a/tests/dummy/mirage/models/github-compare.js b/mirage-support/models/github-compare.js similarity index 100% rename from tests/dummy/mirage/models/github-compare.js rename to mirage-support/models/github-compare.js diff --git a/tests/dummy/mirage/models/github-file.js b/mirage-support/models/github-file.js similarity index 100% rename from tests/dummy/mirage/models/github-file.js rename to mirage-support/models/github-file.js diff --git a/tests/dummy/mirage/models/github-organization.js b/mirage-support/models/github-organization.js similarity index 100% rename from tests/dummy/mirage/models/github-organization.js rename to mirage-support/models/github-organization.js diff --git a/tests/dummy/mirage/models/github-pull.js b/mirage-support/models/github-pull.js similarity index 100% rename from tests/dummy/mirage/models/github-pull.js rename to mirage-support/models/github-pull.js diff --git a/tests/dummy/mirage/models/github-release.js b/mirage-support/models/github-release.js similarity index 100% rename from tests/dummy/mirage/models/github-release.js rename to mirage-support/models/github-release.js diff --git a/tests/dummy/mirage/models/github-repository.js b/mirage-support/models/github-repository.js similarity index 100% rename from tests/dummy/mirage/models/github-repository.js rename to mirage-support/models/github-repository.js diff --git a/tests/dummy/mirage/models/github-user.js b/mirage-support/models/github-user.js similarity index 100% rename from tests/dummy/mirage/models/github-user.js rename to mirage-support/models/github-user.js diff --git a/tests/dummy/mirage/serializers/application.js b/mirage-support/serializers/application.js similarity index 100% rename from tests/dummy/mirage/serializers/application.js rename to mirage-support/serializers/application.js diff --git a/tests/dummy/mirage/serializers/github-compare.js b/mirage-support/serializers/github-compare.js similarity index 100% rename from tests/dummy/mirage/serializers/github-compare.js rename to mirage-support/serializers/github-compare.js diff --git a/tests/dummy/mirage/serializers/github-pull.js b/mirage-support/serializers/github-pull.js similarity index 100% rename from tests/dummy/mirage/serializers/github-pull.js rename to mirage-support/serializers/github-pull.js diff --git a/tests/dummy/mirage/serializers/github-release.js b/mirage-support/serializers/github-release.js similarity index 100% rename from tests/dummy/mirage/serializers/github-release.js rename to mirage-support/serializers/github-release.js diff --git a/tests/dummy/mirage/serializers/github-repository.js b/mirage-support/serializers/github-repository.js similarity index 100% rename from tests/dummy/mirage/serializers/github-repository.js rename to mirage-support/serializers/github-repository.js diff --git a/package.json b/package.json index 4f8cc87..ea26c10 100644 --- a/package.json +++ b/package.json @@ -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": {