Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Fixed missing Unsplash icons when API doesn't return an unsplash sett…
Browse files Browse the repository at this point in the history
…ing (#865)

closes TryGhost/Ghost#9031

- add a default value `unsplash` value to the `setting` model so that Unsplash is activated when the server doesn't return an `unsplash` setting
- update the `unsplash-settings` transform to always deserialize or serialize to `{isActive: true}` when the value is blank or not parsable
- add acceptance regression test covering API not returning an `unplash` setting
- add unit tests for the `unsplash-settings` transform
  • Loading branch information
kevinansfield authored and kirrg001 committed Sep 21, 2017
1 parent 871a104 commit 92e43b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/setting.js
Expand Up @@ -25,5 +25,5 @@ export default Model.extend(ValidationEngine, {
password: attr('string'),
slack: attr('slack-settings'),
amp: attr('boolean'),
unsplash: attr('unsplash-settings')
unsplash: attr('unsplash-settings', {defaultValue: {isActive: true}})
});
12 changes: 8 additions & 4 deletions app/transforms/unsplash-settings.js
Expand Up @@ -2,23 +2,27 @@
import Transform from 'ember-data/transform';
import UnsplashObject from 'ghost-admin/models/unsplash-integration';

const DEFAULT_SETTINGS = {
isActive: true
};

export default Transform.extend({
deserialize(serialized) {
if (serialized) {
let settingsObject;
try {
settingsObject = JSON.parse(serialized) || {};
settingsObject = JSON.parse(serialized) || DEFAULT_SETTINGS;
} catch (e) {
settingsObject = {};
settingsObject = DEFAULT_SETTINGS;
}

return UnsplashObject.create(settingsObject);
}

return null;
return DEFAULT_SETTINGS;
},

serialize(deserialized) {
return deserialized ? JSON.stringify(deserialized) : {};
return deserialized ? JSON.stringify(deserialized) : JSON.stringify(DEFAULT_SETTINGS);
}
});
11 changes: 11 additions & 0 deletions tests/acceptance/editor-test.js
Expand Up @@ -763,5 +763,16 @@ describe('Acceptance: Editor', function() {
'facebook title not present after closing subview'
).to.equal(0);
});

it('has unsplash icon when server doesn\'t return unsplash settings key', async function () {
server.createList('post', 1);

await visit('/editor/1');

expect(
find('.editor-toolbar .fa-camera'),
'unsplash toolbar button'
).to.exist;
});
});
});
46 changes: 46 additions & 0 deletions tests/unit/transforms/unsplash-settings-test.js
@@ -0,0 +1,46 @@
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {setupTest} from 'ember-mocha';

describe('Unit: Transform: unsplash-settings', function () {
setupTest('transform:unsplash-settings', {
// Specify the other units that are required for this test.
// needs: ['transform:foo']
});

it('deserializes to default value when null', function () {
let serialized = null;
let result = this.subject().deserialize(serialized);
expect(result.isActive).to.be.true;
});

it('deserializes to default value when blank string', function () {
let serialized = '';
let result = this.subject().deserialize(serialized);
expect(result.isActive).to.be.true;
});

it('deserializes to default value when invalid JSON', function () {
let serialized = 'not JSON';
let result = this.subject().deserialize(serialized);
expect(result.isActive).to.be.true;
});

it('deserializes valid JSON object', function () {
let serialized = '{"isActive":false}';
let result = this.subject().deserialize(serialized);
expect(result.isActive).to.be.false;
});

it('serializes to JSON string', function () {
let deserialized = {isActive: false};
let result = this.subject().serialize(deserialized);
expect(result).to.equal('{"isActive":false}');
});

it('serializes to default value when blank', function () {
let deserialized = '';
let result = this.subject().serialize(deserialized);
expect(result).to.equal('{"isActive":true}');
});
});

0 comments on commit 92e43b7

Please sign in to comment.