Continuation of #6207 (convert feature controller to service)

refs #6207, #6207
- updates tests for new async behaviour
- fixes tests failing on validation errors
- fixes `feature.labs` not updating after successful save
This commit is contained in:
Kevin Ansfield 2016-02-02 17:05:25 +00:00 committed by Austin Burdine
parent 1e3adef145
commit 648d0043e2
3 changed files with 103 additions and 30 deletions

View File

@ -48,7 +48,7 @@ export default Service.extend({
} }
}, },
labs: computed(function () { labs: computed('_settings', function () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.get('_settings')) { // So we don't query the backend every single time if (this.get('_settings')) { // So we don't query the backend every single time
resolve(this._parseLabs(this.get('_settings'))); resolve(this._parseLabs(this.get('_settings')));
@ -72,8 +72,9 @@ export default Service.extend({
set(labs, key, value); set(labs, key, value);
settings.set('labs', JSON.stringify(labs)); settings.set('labs', JSON.stringify(labs));
settings.save().then(() => { settings.save().then((savedSettings) => {
resolve(labs[key]); this.set('_settings', savedSettings);
resolve(this._parseLabs(savedSettings).get(key));
}).catch((errors) => { }).catch((errors) => {
this.get('notifications').showErrors(errors); this.get('notifications').showErrors(errors);
settings.rollbackAttributes(); settings.rollbackAttributes();

View File

@ -10,12 +10,21 @@ import wait from 'ember-test-helpers/wait';
function stubSettings(server, labs) { function stubSettings(server, labs) {
server.get('/ghost/api/v0.1/settings/', function () { server.get('/ghost/api/v0.1/settings/', function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [{ return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [
id: '1', {
type: 'blog', id: '1',
key: 'labs', type: 'blog',
value: JSON.stringify(labs) key: 'labs',
}]})]; value: JSON.stringify(labs)
},
// postsPerPage is needed to satisfy the validation
{
id: '2',
type: 'blog',
key: 'postsPerPage',
value: 1
}
]})];
}); });
server.put('/ghost/api/v0.1/settings/', function (request) { server.put('/ghost/api/v0.1/settings/', function (request) {

View File

@ -5,15 +5,27 @@ import {
import Pretender from 'pretender'; import Pretender from 'pretender';
import wait from 'ember-test-helpers/wait'; import wait from 'ember-test-helpers/wait';
import FeatureService, {feature} from 'ghost/services/feature'; import FeatureService, {feature} from 'ghost/services/feature';
import Ember from 'ember';
const {merge, run} = Ember;
function stubSettings(server, labs) { function stubSettings(server, labs) {
server.get('/ghost/api/v0.1/settings/', function () { server.get('/ghost/api/v0.1/settings/', function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [{ return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [
id: '1', {
type: 'blog', id: '1',
key: 'labs', type: 'blog',
value: JSON.stringify(labs) key: 'labs',
}]})]; value: JSON.stringify(labs)
},
// postsPerPage is needed to satisfy the validation
{
id: '2',
type: 'blog',
key: 'postsPerPage',
value: 1
}
]})];
}); });
server.put('/ghost/api/v0.1/settings/', function (request) { server.put('/ghost/api/v0.1/settings/', function (request) {
@ -49,8 +61,8 @@ describeModule(
let service = this.subject(); let service = this.subject();
return wait().then(() => { service.get('labs').then((labs) => {
expect(service.get('labs').testFlag).to.be.true; expect(labs.testFlag).to.be.true;
done(); done();
}); });
}); });
@ -62,9 +74,19 @@ describeModule(
let service = this.subject(); let service = this.subject();
service.get('config').set('testFlag', false); service.get('config').set('testFlag', false);
let testFlag, labsTestFlag;
service.get('testFlag').then((result) => {
testFlag = result;
});
service.get('labs').then((labs) => {
labsTestFlag = labs.testFlag;
});
return wait().then(() => { return wait().then(() => {
expect(service.get('labs').testFlag).to.be.false; expect(labsTestFlag).to.be.false;
expect(service.get('testFlag')).to.be.false; expect(testFlag).to.be.false;
done(); done();
}); });
}); });
@ -76,9 +98,19 @@ describeModule(
let service = this.subject(); let service = this.subject();
service.get('config').set('testFlag', true); service.get('config').set('testFlag', true);
let testFlag, labsTestFlag;
service.get('testFlag').then((result) => {
testFlag = result;
});
service.get('labs').then((labs) => {
labsTestFlag = labs.testFlag;
});
return wait().then(() => { return wait().then(() => {
expect(service.get('labs').testFlag).to.be.false; expect(labsTestFlag).to.be.false;
expect(service.get('testFlag')).to.be.true; expect(testFlag).to.be.true;
done(); done();
}); });
}); });
@ -90,9 +122,19 @@ describeModule(
let service = this.subject(); let service = this.subject();
service.get('config').set('testFlag', false); service.get('config').set('testFlag', false);
let testFlag, labsTestFlag;
service.get('testFlag').then((result) => {
testFlag = result;
});
service.get('labs').then((labs) => {
labsTestFlag = labs.testFlag;
});
return wait().then(() => { return wait().then(() => {
expect(service.get('labs').testFlag).to.be.true; expect(labsTestFlag).to.be.true;
expect(service.get('testFlag')).to.be.true; expect(testFlag).to.be.true;
done(); done();
}); });
}); });
@ -104,9 +146,19 @@ describeModule(
let service = this.subject(); let service = this.subject();
service.get('config').set('testFlag', true); service.get('config').set('testFlag', true);
let testFlag, labsTestFlag;
service.get('testFlag').then((result) => {
testFlag = result;
});
service.get('labs').then((labs) => {
labsTestFlag = labs.testFlag;
});
return wait().then(() => { return wait().then(() => {
expect(service.get('labs').testFlag).to.be.true; expect(labsTestFlag).to.be.true;
expect(service.get('testFlag')).to.be.true; expect(testFlag).to.be.true;
done(); done();
}); });
}); });
@ -117,17 +169,28 @@ describeModule(
let service = this.subject(); let service = this.subject();
return wait().then(() => { run(() => {
expect(service.get('testFlag')).to.be.false; service.get('testFlag').then((testFlag) => {
expect(testFlag).to.be.false;
});
});
run(() => {
service.set('testFlag', true); service.set('testFlag', true);
});
return wait().then(() => { return wait().then(() => {
expect(server.handlers[1].numberOfCalls).to.equal(1); expect(server.handlers[1].numberOfCalls).to.equal(1);
expect(service.get('testFlag')).to.be.true;
// TODO: failing because service.update only sets values on
service.get('testFlag').then((testFlag) => {
expect(testFlag).to.be.true;
done(); done();
}); });
}); });
}); });
it('notifies for server errors');
it('notifies for validation errors');
} }
); );