Skip to content

Commit f9e023b

Browse files
committed
Ember Data: model and model-test generators.
1 parent 028a422 commit f9e023b

File tree

13 files changed

+561
-40
lines changed

13 files changed

+561
-40
lines changed

blueprints/model-test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-env node */
2+
3+
var ModelBlueprint = require('../model');
4+
var testInfo = require('ember-cli-test-info');
5+
var useTestFrameworkDetector = require('../test-framework-detector');
6+
7+
module.exports = useTestFrameworkDetector({
8+
description: 'Generates a model unit test.',
9+
10+
locals: function(options) {
11+
var result = ModelBlueprint.locals.apply(this, arguments);
12+
13+
result.friendlyTestDescription = testInfo.description(options.entity.name, "Unit", "Model");
14+
15+
return result;
16+
}
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
import { setupModelTest } from 'ember-mocha';
4+
5+
describe('<%= friendlyTestDescription %>', function() {
6+
setupModelTest('<%= dasherizedModuleName %>', {
7+
// Specify the other units that are required for this test.
8+
<%= typeof needs !== 'undefined' ? needs : '' %>
9+
});
10+
11+
// Replace this with your real tests.
12+
it('exists', function() {
13+
let model = this.subject();
14+
// var store = this.store();
15+
expect(model).to.be.ok;
16+
});
17+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { moduleForModel, test } from 'ember-qunit';
2+
3+
moduleForModel('<%= dasherizedModuleName %>', '<%= friendlyTestDescription %>', {
4+
// Specify the other units that are required for this test.
5+
<%= typeof needs !== 'undefined' ? needs : '' %>
6+
});
7+
8+
test('it exists', function(assert) {
9+
let model = this.subject();
10+
// let store = this.store();
11+
assert.ok(!!model);
12+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
import { run } from '@ember/runloop';
4+
5+
module('<%= friendlyTestDescription %>', function(hooks) {
6+
setupTest(hooks);
7+
8+
// Replace this with your real tests.
9+
test('it exists', function(assert) {
10+
let store = this.owner.lookup('service:store');
11+
let model = run(() => store.createRecord('<%= dasherizedModuleName %>', {}));
12+
assert.ok(model);
13+
});
14+
});

node-tests/blueprints/adapter-test.js

Lines changed: 115 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,126 @@
33
const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
44
const setupTestHooks = blueprintHelpers.setupTestHooks;
55
const emberNew = blueprintHelpers.emberNew;
6+
const emberGenerate = blueprintHelpers.emberGenerate;
67
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
8+
const modifyPackages = blueprintHelpers.modifyPackages;
79

8-
const expect = require('ember-cli-blueprint-test-helpers/chai').expect;
10+
const chai = require('ember-cli-blueprint-test-helpers/chai');
11+
const expect = chai.expect;
912

10-
describe('Acceptance: ember generate and destroy adapter', function() {
13+
const SilentError = require('silent-error');
14+
15+
const generateFakePackageManifest = require('../helpers/generate-fake-package-manifest');
16+
const fixture = require('../helpers/fixture');
17+
18+
describe('Acceptance: generate and destroy adapter blueprints', function() {
1119
setupTestHooks(this);
1220

13-
it('adapter foo-bar', function() {
14-
let args = ['adapter', 'foo-bar'];
21+
beforeEach(function() {
22+
return emberNew();
23+
});
24+
25+
26+
it('adapter', function() {
27+
let args = ['adapter', 'foo'];
28+
29+
return emberGenerateDestroy(args, _file => {
30+
expect(_file('app/adapters/foo.js'))
31+
.to.contain('import DS from \'ember-data\';')
32+
.to.contain('export default DS.JSONAPIAdapter.extend({');
33+
34+
expect(_file('tests/unit/adapters/foo-test.js'))
35+
.to.equal(fixture('adapter-test/foo-default.js'));
36+
});
37+
});
38+
39+
it('adapter extends application adapter if it exists', function() {
40+
let args = ['adapter', 'foo'];
41+
42+
return emberGenerate(['adapter', 'application'])
43+
.then(() => emberGenerateDestroy(args, _file => {
44+
expect(_file('app/adapters/foo.js'))
45+
.to.contain('import ApplicationAdapter from \'./application\';')
46+
.to.contain('export default ApplicationAdapter.extend({');
47+
48+
expect(_file('tests/unit/adapters/foo-test.js'))
49+
.to.equal(fixture('adapter-test/foo-default.js'));
50+
}));
51+
});
52+
53+
it('adapter with --base-class', function() {
54+
let args = ['adapter', 'foo', '--base-class=bar'];
55+
56+
return emberGenerateDestroy(args, _file => {
57+
expect(_file('app/adapters/foo.js'))
58+
.to.contain('import BarAdapter from \'./bar\';')
59+
.to.contain('export default BarAdapter.extend({');
60+
61+
expect(_file('tests/unit/adapters/foo-test.js'))
62+
.to.equal(fixture('adapter-test/foo-default.js'));
63+
});
64+
});
65+
66+
xit('adapter throws when --base-class is same as name', function() {
67+
let args = ['adapter', 'foo', '--base-class=foo'];
1568

16-
// pass any additional command line options in the arguments array
17-
return emberNew().then(() =>
18-
emberGenerateDestroy(args, file => {
19-
const generated = file('app/adapters/foo-bar.ts');
20-
expect(generated).to.contain('export default class FooBar extends DS.JSONAPIAdapter');
21-
expect(generated).to.contain('interface AdapterRegistry');
22-
expect(generated).to.contain("'foo-bar': FooBar");
23-
})
24-
);
69+
return expect(emberGenerate(args))
70+
.to.be.rejectedWith(SilentError, /Adapters cannot extend from themself/);
2571
});
72+
73+
it('adapter when is named "application"', function() {
74+
let args = ['adapter', 'application'];
75+
76+
return emberGenerateDestroy(args, _file => {
77+
expect(_file('app/adapters/application.js'))
78+
.to.contain('import DS from \'ember-data\';')
79+
.to.contain('export default DS.JSONAPIAdapter.extend({');
80+
81+
expect(_file('tests/unit/adapters/application-test.js'))
82+
.to.equal(fixture('adapter-test/application-default.js'));
83+
});
84+
});
85+
86+
it('adapter-test', function() {
87+
let args = ['adapter-test', 'foo'];
88+
89+
return emberGenerateDestroy(args, _file => {
90+
expect(_file('tests/unit/adapters/foo-test.js'))
91+
.to.equal(fixture('adapter-test/foo-default.js'));
92+
});
93+
});
94+
95+
describe('adapter-test with ember-cli-qunit@4.2.0', function() {
96+
beforeEach(function() {
97+
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
98+
});
99+
100+
it('adapter-test-test foo', function() {
101+
return emberGenerateDestroy(['adapter-test', 'foo'], _file => {
102+
expect(_file('tests/unit/adapters/foo-test.js'))
103+
.to.equal(fixture('adapter-test/rfc232.js'));
104+
});
105+
});
106+
});
107+
108+
109+
describe('with ember-cli-mocha v0.12+', function() {
110+
beforeEach(function() {
111+
modifyPackages([
112+
{ name: 'ember-cli-qunit', delete: true },
113+
{ name: 'ember-cli-mocha', dev: true }
114+
]);
115+
generateFakePackageManifest('ember-cli-mocha', '0.12.0');
116+
});
117+
118+
it('adapter-test for mocha v0.12+', function() {
119+
let args = ['adapter-test', 'foo'];
120+
121+
return emberGenerateDestroy(args, _file => {
122+
expect(_file('tests/unit/adapters/foo-test.js'))
123+
.to.equal(fixture('adapter-test/foo-mocha-0.12.js'));
124+
});
125+
});
126+
});
127+
26128
});

node-tests/blueprints/model-test.js

Lines changed: 124 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,133 @@ const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
44
const setupTestHooks = blueprintHelpers.setupTestHooks;
55
const emberNew = blueprintHelpers.emberNew;
66
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
7+
const modifyPackages = blueprintHelpers.modifyPackages;
78

8-
const expect = require('ember-cli-blueprint-test-helpers/chai').expect;
9+
const chai = require('ember-cli-blueprint-test-helpers/chai');
10+
const expect = chai.expect;
911

10-
describe('Acceptance: ember generate and destroy model', function() {
12+
const generateFakePackageManifest = require('../helpers/generate-fake-package-manifest');
13+
const fixture = require('../helpers/fixture');
14+
15+
describe('Acceptance: generate and destroy model blueprints', function() {
1116
setupTestHooks(this);
1217

13-
it('model foo-bar', function() {
14-
let args = ['model', 'foo-bar'];
15-
16-
// pass any additional command line options in the arguments array
17-
return emberNew().then(() =>
18-
emberGenerateDestroy(args, file => {
19-
const generated = file('app/models/foo-bar.ts');
20-
expect(generated).to.contain('export default class FooBar extends DS.Model');
21-
expect(generated).to.contain('interface ModelRegistry');
22-
expect(generated).to.contain("'foo-bar': FooBar");
23-
})
24-
);
18+
beforeEach(function() {
19+
return emberNew();
20+
});
21+
22+
it('model', function() {
23+
let args = ['model', 'foo'];
24+
25+
return emberGenerateDestroy(args, _file => {
26+
expect(_file('app/models/foo.ts'))
27+
.to.contain("import DS from 'ember-data';")
28+
.to.contain('export default class Foo extends DS.Model.extend(');
29+
30+
expect(_file('tests/unit/models/foo-test.ts')).to.equal(fixture('model-test/foo-default.ts'));
31+
});
32+
});
33+
34+
it('model with attrs', function() {
35+
let args = [
36+
'model',
37+
'foo',
38+
'misc',
39+
'skills:array',
40+
'isActive:boolean',
41+
'birthday:date',
42+
'someObject:object',
43+
'age:number',
44+
'name:string',
45+
'customAttr:custom-transform',
46+
];
47+
48+
return emberGenerateDestroy(args, _file => {
49+
expect(_file('app/models/foo.ts'))
50+
.to.contain("import DS from 'ember-data';")
51+
.to.contain('export default class Foo extends DS.Model.extend(')
52+
.to.contain('misc: DS.attr()')
53+
.to.contain("skills: DS.attr('array')")
54+
.to.contain("isActive: DS.attr('boolean')")
55+
.to.contain("birthday: DS.attr('date')")
56+
.to.contain("someObject: DS.attr('object')")
57+
.to.contain("age: DS.attr('number')")
58+
.to.contain("name: DS.attr('string')")
59+
.to.contain("customAttr: DS.attr('custom-transform')");
60+
61+
expect(_file('tests/unit/models/foo-test.ts')).to.equal(fixture('model-test/foo-default.ts'));
62+
});
63+
});
64+
65+
it('model with belongsTo', function() {
66+
let args = ['model', 'comment', 'post:belongs-to', 'author:belongs-to:user'];
67+
68+
return emberGenerateDestroy(args, _file => {
69+
expect(_file('app/models/comment.ts'))
70+
.to.contain("import DS from 'ember-data';")
71+
.to.contain('export default class Comment extends DS.Model.extend(')
72+
.to.contain("post: DS.belongsTo('post')")
73+
.to.contain("author: DS.belongsTo('user')");
74+
75+
expect(_file('tests/unit/models/comment-test.ts')).to.equal(
76+
fixture('model-test/comment-default.ts')
77+
);
78+
});
79+
});
80+
81+
it('model with hasMany', function() {
82+
let args = ['model', 'post', 'comments:has-many', 'otherComments:has-many:comment'];
83+
84+
return emberGenerateDestroy(args, _file => {
85+
expect(_file('app/models/post.ts'))
86+
.to.contain("import DS from 'ember-data';")
87+
.to.contain('export default class Post extends DS.Model.extend(')
88+
.to.contain("comments: DS.hasMany('comment')")
89+
.to.contain("otherComments: DS.hasMany('comment')");
90+
91+
expect(_file('tests/unit/models/post-test.ts')).to.equal(
92+
fixture('model-test/post-default.ts')
93+
);
94+
});
95+
});
96+
97+
it('model-test', function() {
98+
let args = ['model-test', 'foo'];
99+
100+
return emberGenerateDestroy(args, _file => {
101+
expect(_file('tests/unit/models/foo-test.ts')).to.equal(fixture('model-test/foo-default.ts'));
102+
});
103+
});
104+
105+
describe('model-test with ember-cli-qunit@4.2.0', function() {
106+
beforeEach(function() {
107+
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
108+
});
109+
110+
it('model-test-test foo', function() {
111+
return emberGenerateDestroy(['model-test', 'foo'], _file => {
112+
expect(_file('tests/unit/models/foo-test.ts')).to.equal(fixture('model-test/rfc232.ts'));
113+
});
114+
});
115+
});
116+
117+
describe('with ember-cli-mocha v0.12+', function() {
118+
beforeEach(function() {
119+
modifyPackages([
120+
{ name: 'ember-cli-qunit', delete: true },
121+
{ name: 'ember-cli-mocha', dev: true },
122+
]);
123+
generateFakePackageManifest('ember-cli-mocha', '0.12.0');
124+
});
125+
126+
it('model-test for mocha v0.12+', function() {
127+
let args = ['model-test', 'foo'];
128+
129+
return emberGenerateDestroy(args, _file => {
130+
expect(_file('tests/unit/models/foo-test.ts')).to.equal(
131+
fixture('model-test/foo-mocha-0.12.ts')
132+
);
133+
});
134+
});
25135
});
26136
});

0 commit comments

Comments
 (0)