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

Add tests for discoverEmberDataModels function #2528

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions packages/ember-cli-mirage/addon/ember-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global requirejs */
/* global global,requirejs */

import require from 'require';
import config from 'ember-get-config';
Expand All @@ -14,6 +14,27 @@ const { modulePrefix, podModulePrefix } = config;
let DsModels, Models;
let DsSerializers, Serializers;

/**
* Gets application from global context, set inside initializers/ember-cli-mirage
*
* @method getApplication
* @private
* @hide
* @return {Object} Application
*/
function getApplication() {
let theGlobal = window || global || self;

try {
return theGlobal.emberCliMirageApplication;
} catch (err) {
throw new Error(
'ember-cli-mirage/ember-data | global "emberCliMirageApplication" could not be found: ',
err
);
}
}

/**
* Get all ember data models under the app's namespaces
*
Expand All @@ -27,6 +48,16 @@ export function getDsModels() {
return DsModels;
}

let application = getApplication();

/*
applicationInstance.lookup('service:store') should've been used instead
but ember-cli-mirage/ember-data.js runs before any instance-initializers
so there is no way to get hold of the applicationInstance before this code executes,
at least not with the current setup
*/
let store = application.__container__.lookup('service:store');

let moduleMap = requirejs.entries;
let classicModelMatchRegex = new RegExp(`^${modulePrefix}/models/(.*)$`, 'i');
let podModelMatchRegex = new RegExp(
Expand All @@ -46,7 +77,7 @@ export function getDsModels() {
if (matches && matches[1]) {
let modelName = matches[1];

let model = require(path, null, null, true).default;
let model = store.modelFor(modelName);
if (isDsModel(model)) {
DsModels[modelName] = model;
}
Expand Down
21 changes: 21 additions & 0 deletions packages/ember-cli-mirage/app/initializers/ember-cli-mirage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global global */
import ENV from '../config/environment';
import getRfc232TestContext from 'ember-cli-mirage/get-rfc232-test-context';
import startMirageImpl from 'ember-cli-mirage/start-mirage';
Expand All @@ -16,6 +17,8 @@ const { default: makeServer } = config;
export default {
name: 'ember-cli-mirage',
initialize(application) {
setApplicationGlobal(application);

if (makeServer) {
application.register('mirage:make-server', makeServer, {
instantiate: false,
Expand Down Expand Up @@ -56,3 +59,21 @@ function _defaultEnabled(env, addonConfig) {

return usingInDev || usingInTest;
}

/*
Attaches `Application` to the global context (e.g. window)
so that ember-cli-mirage/ember-data.js can then use it to retrieve
the Models from the Application's store
*/
function setApplicationGlobal(application) {
let theGlobal = window || global || self;

try {
theGlobal.emberCliMirageApplication = application;
} catch (err) {
throw new Error(
'ember-cli-mirage/initializers/ember-cli-mirage | could not set application global: ',
err
);
}
}
6 changes: 0 additions & 6 deletions packages/ember-cli-mirage/tests/dummy/app/models/comment.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import Model, { attr, belongsTo } from '@ember-data/model';
import compileMarkdown from 'ember-cli-addon-docs/utils/compile-markdown';
import { htmlSafe } from '@ember/template';

export default class Comment extends Model {
@belongsTo() user;

@attr body;
@attr permalink;
@attr createdAt;

get htmlBody() {
return htmlSafe(compileMarkdown(this.body));
}
}
6 changes: 0 additions & 6 deletions packages/ember-cli-mirage/tests/dummy/app/models/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Model, { attr, hasMany } from '@ember-data/model';
import yaml from 'js-yaml';
import compileMarkdown from 'ember-cli-addon-docs/utils/compile-markdown';
import { htmlSafe } from '@ember/template';

export default class Post extends Model {
@hasMany() comments;
Expand All @@ -10,10 +8,6 @@ export default class Post extends Model {
@attr body;
@attr issueUrl;

get htmlBody() {
return htmlSafe(compileMarkdown(this.body));
}

get meta() {
let lines = this.body.split('\n').map((line) => line.trim());
let firstLine = lines[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { createServer } from 'miragejs';
import { discoverEmberDataModels } from 'ember-cli-mirage';

module('Unit | Model | ember data model discover', function (hooks) {
setupTest(hooks);

let server;

hooks.beforeEach(function () {
server = createServer({
models: discoverEmberDataModels({}),
});
});

hooks.afterEach(function () {
server.shutdown();
});

test('it discovers the models', function (assert) {
assert.ok(server.schema.modelFor('address'));
assert.ok(server.schema.modelFor('comment'));
assert.ok(server.schema.modelFor('post'));
assert.ok(server.schema.modelFor('user'));
assert.ok(server.schema.modelFor('wordSmith'));
});
});
Loading