From 648d0043e2834addff440ee2fde5fb8daeb717f2 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Tue, 2 Feb 2016 17:05:25 +0000 Subject: [PATCH] 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 --- ghost/admin/app/services/feature.js | 7 +- .../components/gh-feature-flag-test.js | 21 +++- .../integration/services/feature-test.js | 105 ++++++++++++++---- 3 files changed, 103 insertions(+), 30 deletions(-) diff --git a/ghost/admin/app/services/feature.js b/ghost/admin/app/services/feature.js index ccd5993d01..fd5f45a57c 100644 --- a/ghost/admin/app/services/feature.js +++ b/ghost/admin/app/services/feature.js @@ -48,7 +48,7 @@ export default Service.extend({ } }, - labs: computed(function () { + labs: computed('_settings', function () { return new Promise((resolve, reject) => { if (this.get('_settings')) { // So we don't query the backend every single time resolve(this._parseLabs(this.get('_settings'))); @@ -72,8 +72,9 @@ export default Service.extend({ set(labs, key, value); settings.set('labs', JSON.stringify(labs)); - settings.save().then(() => { - resolve(labs[key]); + settings.save().then((savedSettings) => { + this.set('_settings', savedSettings); + resolve(this._parseLabs(savedSettings).get(key)); }).catch((errors) => { this.get('notifications').showErrors(errors); settings.rollbackAttributes(); diff --git a/ghost/admin/tests/integration/components/gh-feature-flag-test.js b/ghost/admin/tests/integration/components/gh-feature-flag-test.js index 5a19ad1d97..2552b8bfb2 100644 --- a/ghost/admin/tests/integration/components/gh-feature-flag-test.js +++ b/ghost/admin/tests/integration/components/gh-feature-flag-test.js @@ -10,12 +10,21 @@ import wait from 'ember-test-helpers/wait'; function stubSettings(server, labs) { server.get('/ghost/api/v0.1/settings/', function () { - return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [{ - id: '1', - type: 'blog', - key: 'labs', - value: JSON.stringify(labs) - }]})]; + return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [ + { + id: '1', + type: 'blog', + 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) { diff --git a/ghost/admin/tests/integration/services/feature-test.js b/ghost/admin/tests/integration/services/feature-test.js index 0e3e8a0268..0e93a8d9fa 100644 --- a/ghost/admin/tests/integration/services/feature-test.js +++ b/ghost/admin/tests/integration/services/feature-test.js @@ -5,15 +5,27 @@ import { import Pretender from 'pretender'; import wait from 'ember-test-helpers/wait'; import FeatureService, {feature} from 'ghost/services/feature'; +import Ember from 'ember'; + +const {merge, run} = Ember; function stubSettings(server, labs) { server.get('/ghost/api/v0.1/settings/', function () { - return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [{ - id: '1', - type: 'blog', - key: 'labs', - value: JSON.stringify(labs) - }]})]; + return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [ + { + id: '1', + type: 'blog', + 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) { @@ -49,8 +61,8 @@ describeModule( let service = this.subject(); - return wait().then(() => { - expect(service.get('labs').testFlag).to.be.true; + service.get('labs').then((labs) => { + expect(labs.testFlag).to.be.true; done(); }); }); @@ -62,9 +74,19 @@ describeModule( let service = this.subject(); 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(() => { - expect(service.get('labs').testFlag).to.be.false; - expect(service.get('testFlag')).to.be.false; + expect(labsTestFlag).to.be.false; + expect(testFlag).to.be.false; done(); }); }); @@ -76,9 +98,19 @@ describeModule( let service = this.subject(); 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(() => { - expect(service.get('labs').testFlag).to.be.false; - expect(service.get('testFlag')).to.be.true; + expect(labsTestFlag).to.be.false; + expect(testFlag).to.be.true; done(); }); }); @@ -90,9 +122,19 @@ describeModule( let service = this.subject(); 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(() => { - expect(service.get('labs').testFlag).to.be.true; - expect(service.get('testFlag')).to.be.true; + expect(labsTestFlag).to.be.true; + expect(testFlag).to.be.true; done(); }); }); @@ -104,9 +146,19 @@ describeModule( let service = this.subject(); 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(() => { - expect(service.get('labs').testFlag).to.be.true; - expect(service.get('testFlag')).to.be.true; + expect(labsTestFlag).to.be.true; + expect(testFlag).to.be.true; done(); }); }); @@ -117,17 +169,28 @@ describeModule( let service = this.subject(); - return wait().then(() => { - expect(service.get('testFlag')).to.be.false; + run(() => { + service.get('testFlag').then((testFlag) => { + expect(testFlag).to.be.false; + }); + }); + run(() => { service.set('testFlag', true); + }); - return wait().then(() => { - expect(server.handlers[1].numberOfCalls).to.equal(1); - expect(service.get('testFlag')).to.be.true; + return wait().then(() => { + expect(server.handlers[1].numberOfCalls).to.equal(1); + + // TODO: failing because service.update only sets values on + service.get('testFlag').then((testFlag) => { + expect(testFlag).to.be.true; done(); }); }); }); + + it('notifies for server errors'); + it('notifies for validation errors'); } );