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

blueprints/serializer-test: Add RFC232 variants #5298

Merged
merged 4 commits into from
Dec 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { run } from '@ember/runloop';

module('serializer:<%= dasherizedModuleName %>', '<%= friendlyTestDescription %>', function(hooks) {
Copy link
Member

Choose a reason for hiding this comment

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

Should only be two arguments here (friendlyTestDescription and the function(hooks) {}...

setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let store = this.owner.lookup('service:store');
let serializer = this.owner.factoryFor('serializer:<%= dasherizedModuleName %>').create({ store });
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be .lookup (not factoryFor().create()) because serializers are singleton...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you need the store initialized on the serializer though, which lookup doesn't automatically do
is that something that should be addressed upstream?

Copy link
Member

Choose a reason for hiding this comment

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

Haha, this seems sorta bonkers. I submitted #5300 to simplify greatly 😺

Copy link
Member

Choose a reason for hiding this comment

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

@alexander-alvarez - Even without #5300, we should do store.serializerFor(<%= dasherizedModelName %>) here as that is the public API for getting a given things serializer. There are a few fallbacks that Ember Data handles nicely for you (e.g. use application if you don't have a model specific one) and we should go through the same common API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

understood 👍


assert.ok(serializer);
});

test('it serializes records', function(assert) {
let store = this.owner.lookup('service:store');
let record = run(() => store.createRecord('<%= dasherizedModuleName %>', {}));

let serializedRecord = record.serialize();

assert.ok(serializedRecord);
Copy link
Member

Choose a reason for hiding this comment

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

This should probably use some sort of assert.deepEqual

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@alexander-alvarez alexander-alvarez Dec 26, 2017

Choose a reason for hiding this comment

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

I was playing around with adding assert.deepEqual({}, serializedRecord); in my repo, and realized that if you've already implemented the model for which a serializer is being generated, at the moment we can't generate a test case that will pass when generated + when using the equality check.

I was under the impression that generated test cases should always pass when initially generated with a new model (e.g. running ember g serializer should generate a serializer and a passing test)-- do we want to continue to have this behavior?

});
});
73 changes: 45 additions & 28 deletions node-tests/blueprints/serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ const fixture = require('../helpers/fixture');
describe('Acceptance: generate and destroy serializer blueprints', function() {
setupTestHooks(this);


beforeEach(function() {
return emberNew();
});

it('serializer', function() {
let args = ['serializer', 'foo'];

return emberNew()
.then(() => emberGenerateDestroy(args, _file => {
return emberGenerateDestroy(args, _file => {
expect(_file('app/serializers/foo.js'))
.to.contain('import DS from \'ember-data\';')
.to.contain('export default DS.JSONAPISerializer.extend(');

expect(_file('tests/unit/serializers/foo-test.js'))
.to.equal(fixture('serializer-test/foo-default.js'));
}));
});
});

it('serializer extends application serializer if it exists', function() {
let args = ['serializer', 'foo'];

return emberNew()
.then(() => emberGenerate(['serializer', 'application']))
return emberGenerate(['serializer', 'application'])
.then(() => emberGenerateDestroy(args, _file => {
expect(_file('app/serializers/foo.js'))
.to.contain('import ApplicationSerializer from \'./application\';')
Expand All @@ -50,61 +53,75 @@ describe('Acceptance: generate and destroy serializer blueprints', function() {
it('serializer with --base-class', function() {
let args = ['serializer', 'foo', '--base-class=bar'];

return emberNew()
.then(() => emberGenerateDestroy(args, _file => {
return emberGenerateDestroy(args, _file => {
expect(_file('app/serializers/foo.js'))
.to.contain('import BarSerializer from \'./bar\';')
.to.contain('export default BarSerializer.extend({');

expect(_file('tests/unit/serializers/foo-test.js'))
.to.equal(fixture('serializer-test/foo-default.js'));
}));
});
});

xit('serializer throws when --base-class is same as name', function() {
let args = ['serializer', 'foo', '--base-class=foo'];

return emberNew()
.then(() => expect(emberGenerate(args))
.to.be.rejectedWith(SilentError, /Serializers cannot extend from themself/));
return expect(emberGenerate(args))
.to.be.rejectedWith(SilentError, /Serializers cannot extend from themself/);
});

it('serializer when is named "application"', function() {
let args = ['serializer', 'application'];

return emberNew()
.then(() => emberGenerateDestroy(args, _file => {
return emberGenerateDestroy(args, _file => {
expect(_file('app/serializers/application.js'))
.to.contain('import DS from \'ember-data\';')
.to.contain('export default DS.JSONAPISerializer.extend({');

expect(_file('tests/unit/serializers/application-test.js'))
.to.equal(fixture('serializer-test/application-default.js'));
}));
});
});

it('serializer-test', function() {
let args = ['serializer-test', 'foo'];

return emberNew()
.then(() => emberGenerateDestroy(args, _file => {
return emberGenerateDestroy(args, _file => {
expect(_file('tests/unit/serializers/foo-test.js'))
.to.equal(fixture('serializer-test/foo-default.js'));
}));
});
});

it('serializer-test for mocha v0.12+', function() {
let args = ['serializer-test', 'foo'];
describe('serializer-test with [email protected]', function() {
beforeEach(function() {
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
});

return emberNew()
.then(() => modifyPackages([
{name: 'ember-cli-qunit', delete: true},
{name: 'ember-cli-mocha', dev: true}
]))
.then(() => generateFakePackageManifest('ember-cli-mocha', '0.12.0'))
.then(() => emberGenerateDestroy(args, _file => {
it('serializer-test-test foo', function() {
return emberGenerateDestroy(['serializer-test', 'foo'], _file => {
expect(_file('tests/unit/serializers/foo-test.js'))
.to.equal(fixture('serializer-test/foo-mocha-0.12.js'));
}));
.to.equal(fixture('serializer-test/rfc232.js'));
});
});
});

describe('with ember-cli-mocha v0.12+', function() {
beforeEach(function() {
modifyPackages([
{ name: 'ember-cli-qunit', delete: true },
{ name: 'ember-cli-mocha', dev: true }
]);
generateFakePackageManifest('ember-cli-mocha', '0.12.0');
});

it('serializer-test for mocha v0.12+', function() {
let args = ['serializer-test', 'foo'];

return emberGenerateDestroy(args, _file => {
expect(_file('tests/unit/serializers/foo-test.js'))
.to.equal(fixture('serializer-test/foo-mocha-0.12.js'));
});
});
});

});
24 changes: 24 additions & 0 deletions node-tests/fixtures/serializer-test/rfc232.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { run } from '@ember/runloop';

module('serializer:foo', 'Unit | Serializer | foo', function(hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let store = this.owner.lookup('service:store');
let serializer = this.owner.factoryFor('serializer:foo').create({ store });

assert.ok(serializer);
});

test('it serializes records', function(assert) {
let store = this.owner.lookup('service:store');
let record = run(() => store.createRecord('foo', {}));

let serializedRecord = record.serialize();

assert.ok(serializedRecord);
});
});