Skip to content

Commit

Permalink
Shutdown pretender servers in unit tests (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinfarris authored and samselikoff committed Oct 13, 2016
1 parent 0967dac commit 2d461aa
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion tests/unit/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,68 @@ module('Unit | Server');

test('it can be instantiated', function(assert) {
let server = new Server({ environment: 'test' });

assert.ok(server);

server.shutdown();
});

test('it runs the default scenario in non-test environments', function(assert) {
assert.expect(1);

new Server({
let server = new Server({
environment: 'development',
scenarios: {
default(server) {
assert.ok(true);
}
}
});

server.shutdown();
});

module('Unit | Server #loadConfig');

test('forces timing to 0 in test environment', function(assert) {
let server = new Server({ environment: 'test' });

server.loadConfig(function() {
this.timing = 50;
});

assert.equal(server.timing, 0);

server.shutdown();
});

test("doesn't modify user's timing config in other environments", function(assert) {
let server = new Server({ environment: 'blah' });

server.loadConfig(function() {
this.timing = 50;
});

assert.equal(server.timing, 50);

server.shutdown();
});

module('Unit | Server #db');

test('its db is isolated across instances', function(assert) {
let server1 = new Server({ environment: 'test' });

server1.db.createCollection('contacts');
server1.db.contacts.insert({ name: 'Sam' });

server1.shutdown();

let server2 = new Server({ environment: 'test' });

assert.equal(server2.contacts, undefined);

server2.shutdown();
});

module('Unit | Server #create');
Expand All @@ -61,6 +79,8 @@ test('create fails when no factories or models are registered', function(assert)
assert.throws(function() {
server.create('contact');
});

server.shutdown();
});

test('create fails when an expected factory isn\'t registered', function(assert) {
Expand All @@ -74,6 +94,8 @@ test('create fails when an expected factory isn\'t registered', function(assert)
assert.throws(function() {
server.create('contact');
}, /no model or factory was found/);

server.shutdown();
});

test('create works when models but no factories are registered', function(assert) {
Expand All @@ -85,7 +107,10 @@ test('create works when models but no factories are registered', function(assert
});

server.create('contact');

assert.equal(server.db.contacts.length, 1);

server.shutdown();
});

test('create adds the data to the db', function(assert) {
Expand All @@ -103,6 +128,8 @@ test('create adds the data to the db', function(assert) {

assert.equal(contactsInDb.length, 1);
assert.deepEqual(contactsInDb[0], { id: '1', name: 'Sam' });

server.shutdown();
});

test('create returns the new data in the db', function(assert) {
Expand All @@ -118,6 +145,8 @@ test('create returns the new data in the db', function(assert) {
let contact = server.create('contact');

assert.deepEqual(contact, { id: '1', name: 'Sam' });

server.shutdown();
});

test('create allows for attr overrides', function(assert) {
Expand All @@ -135,6 +164,8 @@ test('create allows for attr overrides', function(assert) {

assert.deepEqual(sam, { id: '1', name: 'Sam' });
assert.deepEqual(link, { id: '2', name: 'Link' });

server.shutdown();
});

test('create allows for attr overrides with extended factories', function(assert) {
Expand All @@ -161,6 +192,8 @@ test('create allows for attr overrides with extended factories', function(assert

assert.deepEqual(link, { id: '1', name: 'Link', age: 500, is_young: false });
assert.deepEqual(youngLink, { id: '2', name: 'Link', age: 10, is_young: true });

server.shutdown();
});

test('create allows for attr overrides with arrays', function(assert) {
Expand All @@ -180,6 +213,8 @@ test('create allows for attr overrides with arrays', function(assert) {
assert.deepEqual(sam, { id: '1', name: ['Sam', 'Carl'] });
assert.deepEqual(link, { id: '2', name: ['Link'] });
assert.deepEqual(noname, { id: '3', name: [] });

server.shutdown();
});

test('create allows for nested attr overrides', function(assert) {
Expand All @@ -202,6 +237,8 @@ test('create allows for nested attr overrides', function(assert) {

assert.deepEqual(contact1, { id: '1', address: { streetName: 'Main', streetAddress: 1000 } });
assert.deepEqual(contact2, { id: '2', address: { streetName: 'Main', streetAddress: 1001 } });

server.shutdown();
});

test('create allows for arrays of attr overrides', function(assert) {
Expand All @@ -224,6 +261,8 @@ test('create allows for arrays of attr overrides', function(assert) {

assert.deepEqual(contact1, { id: '1', websites: ['http://example.com', 'http://placekitten.com/320/240'] });
assert.deepEqual(contact2, { id: '2', websites: ['http://example.com', 'http://placekitten.com/321/241'] });

server.shutdown();
});

test('create allows to extend factory with trait', function(assert) {
Expand All @@ -249,6 +288,8 @@ test('create allows to extend factory with trait', function(assert) {
assert.deepEqual(article, { id: '1', title: 'Lorem ipsum' });
assert.deepEqual(publishedArticle, { id: '2', title: 'Lorem ipsum', isPublished: true,
publishedAt: '2010-01-01 10:00:00' });

server.shutdown();
});

test('create allows to extend factory with multiple traits', function(assert) {
Expand Down Expand Up @@ -281,6 +322,8 @@ test('create allows to extend factory with multiple traits', function(assert) {
publishedAt: '2010-01-01 10:00:00' });
assert.deepEqual(publishedArticleWithContent, { id: '3', title: 'Lorem ipsum', isPublished: true,
publishedAt: '2010-01-01 10:00:00', content: 'content' });

server.shutdown();
});

test('create allows to extend factory with traits containing afterCreate callbacks', function(assert) {
Expand Down Expand Up @@ -309,6 +352,8 @@ test('create allows to extend factory with traits containing afterCreate callbac

assert.deepEqual(articleWithComments, { id: '1', title: 'Lorem ipsum' });
assert.equal(server.db.comments.length, 3);

server.shutdown();
});

test('create does not execute afterCreate callbacks from traits that are not applied', function(assert) {
Expand Down Expand Up @@ -337,6 +382,8 @@ test('create does not execute afterCreate callbacks from traits that are not app

assert.deepEqual(articleWithComments, { id: '1', title: 'Lorem ipsum' });
assert.equal(server.db.comments.length, 0);

server.shutdown();
});

test('create allows to extend with multiple traits and to apply attr overrides', function(assert) {
Expand Down Expand Up @@ -367,6 +414,8 @@ test('create allows to extend with multiple traits and to apply attr overrides',

assert.deepEqual(publishedArticleWithContent, { id: '1', title: 'Lorem ipsum', isPublished: true,
publishedAt: '2012-01-01 10:00:00', content: 'content' });

server.shutdown();
});

test('create throws errors when using trait that is not defined and distinquishes between traits and non-traits', function(assert) {
Expand All @@ -393,11 +442,16 @@ test('create throws errors when using trait that is not defined and distinquishe
assert.throws(() => {
server.create('article', 'private');
}, /'private' trait is not registered in 'article' factory/);

server.shutdown();
});

module('Unit | Server #createList', {
beforeEach() {
this.server = new Server({ environment: 'test' });
},
afterEach() {
this.server.shutdown();
}
});

Expand Down Expand Up @@ -538,6 +592,9 @@ test('createList throws errors when using trait that is not defined and distinqu
module('Unit | Server #build', {
beforeEach() {
this.server = new Server({ environment: 'test' });
},
afterEach() {
this.server.shutdown();
}
});

Expand Down Expand Up @@ -752,6 +809,9 @@ test('build throws errors when using trait that is not defined and distinquishes
module('Unit | Server #buildList', {
beforeEach() {
this.server = new Server({ environment: 'test' });
},
afterEach() {
this.server.shutdown();
}
});

Expand Down Expand Up @@ -898,6 +958,8 @@ test('server configures default passthroughs when useDefaultPassthroughs is true

assert.ok(isPassedThrough);
});

server.shutdown();
});

test('server does not configure default passthroughs when useDefaultPassthroughs is false', function(assert) {
Expand All @@ -910,4 +972,6 @@ test('server does not configure default passthroughs when useDefaultPassthroughs

assert.ok(!isPassedThrough);
});

server.shutdown();
});

0 comments on commit 2d461aa

Please sign in to comment.