Ghost/ghost/admin/tests/unit/controllers/post-settings-menu-test.js

571 lines
22 KiB
JavaScript
Raw Normal View History

/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
import run from 'ember-runloop';
import RSVP from 'rsvp';
import EmberObject from 'ember-object';
2015-02-18 23:02:48 +03:00
import {
describeModule,
it
} from 'ember-mocha';
Timezones: Always use the timezone of blog setting closes TryGhost/Ghost#6406 follow-up PR of #2 - adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings - `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field. - gets `availableTimezones` from `configuration/timezones` API endpoint - adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside - when switching the timezone in the select box, the user will be shown the local time of the selected timezone - `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already - added clock service to show actual time ticking below select box - default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London' - if no timezone is saved in the settings yet, the default value will be used - shows the local time in 'Publish Date' in PSM by default, until user overwrites it - adds dependency `moment-timezone 0.5.4` to `bower.json` --------- **Tests:** - sets except for clock service in test env - adds fixtures to mirage - adds `service.ajax` and `service:ghostPaths` to navigation-test.js - adds unit test for `gh-format-timeago` helper - updates acceptance test `general-setting` - adds acceptance test for `editor` - adds integration tests for `services/config` and `services/time-zone` --------- **Todos:** - [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input` - [x] Acceptance test: `editor` - [ ] Unit tests: `utils/date-formatting` - [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
import sinon from 'sinon';
2015-02-18 23:02:48 +03:00
function K() {
return this;
}
2015-02-18 23:02:48 +03:00
describeModule(
'controller:post-settings-menu',
'Unit: Controller: post-settings-menu',
2015-02-18 23:02:48 +03:00
{
Timezones: Always use the timezone of blog setting closes TryGhost/Ghost#6406 follow-up PR of #2 - adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings - `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field. - gets `availableTimezones` from `configuration/timezones` API endpoint - adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside - when switching the timezone in the select box, the user will be shown the local time of the selected timezone - `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already - added clock service to show actual time ticking below select box - default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London' - if no timezone is saved in the settings yet, the default value will be used - shows the local time in 'Publish Date' in PSM by default, until user overwrites it - adds dependency `moment-timezone 0.5.4` to `bower.json` --------- **Tests:** - sets except for clock service in test env - adds fixtures to mirage - adds `service.ajax` and `service:ghostPaths` to navigation-test.js - adds unit test for `gh-format-timeago` helper - updates acceptance test `general-setting` - adds acceptance test for `editor` - adds integration tests for `services/config` and `services/time-zone` --------- **Todos:** - [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input` - [x] Acceptance test: `editor` - [ ] Unit tests: `utils/date-formatting` - [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
needs: ['controller:application', 'service:notifications', 'service:slug-generator', 'service:timeZone']
2015-02-18 23:02:48 +03:00
},
function () {
it('slugValue is one-way bound to model.slug', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'a-slug'
})
});
expect(controller.get('model.slug')).to.equal('a-slug');
expect(controller.get('slugValue')).to.equal('a-slug');
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('model.slug', 'changed-slug');
expect(controller.get('slugValue')).to.equal('changed-slug');
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('slugValue', 'changed-directly');
expect(controller.get('model.slug')).to.equal('changed-slug');
expect(controller.get('slugValue')).to.equal('changed-directly');
});
run(function () {
2015-02-18 23:02:48 +03:00
// test that the one-way binding is still in place
controller.set('model.slug', 'should-update');
expect(controller.get('slugValue')).to.equal('should-update');
});
});
it('metaTitleScratch is one-way bound to model.metaTitle', function () {
let controller = this.subject({
model: EmberObject.create({
metaTitle: 'a title'
2015-02-18 23:02:48 +03:00
})
});
expect(controller.get('model.metaTitle')).to.equal('a title');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaTitleScratch')).to.equal('a title');
run(function () {
controller.set('model.metaTitle', 'a different title');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaTitleScratch')).to.equal('a different title');
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('metaTitleScratch', 'changed directly');
expect(controller.get('model.metaTitle')).to.equal('a different title');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaTitleScratch')).to.equal('changed directly');
});
run(function () {
2015-02-18 23:02:48 +03:00
// test that the one-way binding is still in place
controller.set('model.metaTitle', 'should update');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaTitleScratch')).to.equal('should update');
});
});
it('metaDescriptionScratch is one-way bound to model.metaDescription', function () {
let controller = this.subject({
model: EmberObject.create({
metaDescription: 'a description'
2015-02-18 23:02:48 +03:00
})
});
expect(controller.get('model.metaDescription')).to.equal('a description');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaDescriptionScratch')).to.equal('a description');
run(function () {
controller.set('model.metaDescription', 'a different description');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaDescriptionScratch')).to.equal('a different description');
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('metaDescriptionScratch', 'changed directly');
expect(controller.get('model.metaDescription')).to.equal('a different description');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaDescriptionScratch')).to.equal('changed directly');
});
run(function () {
2015-02-18 23:02:48 +03:00
// test that the one-way binding is still in place
controller.set('model.metaDescription', 'should update');
2015-02-18 23:02:48 +03:00
expect(controller.get('metaDescriptionScratch')).to.equal('should update');
});
});
describe('seoTitle', function () {
it('should be the metaTitle if one exists', function () {
let controller = this.subject({
model: EmberObject.create({
metaTitle: 'a meta-title',
2015-02-18 23:02:48 +03:00
titleScratch: 'should not be used'
})
});
expect(controller.get('seoTitle')).to.equal('a meta-title');
});
it('should default to the title if an explicit meta-title does not exist', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
titleScratch: 'should be the meta-title'
})
});
expect(controller.get('seoTitle')).to.equal('should be the meta-title');
});
it('should be the metaTitle if both title and metaTitle exist', function () {
let controller = this.subject({
model: EmberObject.create({
metaTitle: 'a meta-title',
2015-02-18 23:02:48 +03:00
titleScratch: 'a title'
})
});
expect(controller.get('seoTitle')).to.equal('a meta-title');
});
it('should revert to the title if explicit metaTitle is removed', function () {
let controller = this.subject({
model: EmberObject.create({
metaTitle: 'a meta-title',
2015-02-18 23:02:48 +03:00
titleScratch: 'a title'
})
});
expect(controller.get('seoTitle')).to.equal('a meta-title');
run(function () {
controller.set('model.metaTitle', '');
2015-02-18 23:02:48 +03:00
expect(controller.get('seoTitle')).to.equal('a title');
});
});
it('should truncate to 70 characters with an appended ellipsis', function () {
let longTitle = new Array(100).join('a');
let controller = this.subject({
model: EmberObject.create()
2015-02-18 23:02:48 +03:00
});
expect(longTitle.length).to.equal(99);
run(function () {
let expected = `${longTitle.substr(0, 70)}…`;
2015-02-18 23:02:48 +03:00
controller.set('metaTitleScratch', longTitle);
expect(controller.get('seoTitle').toString().length).to.equal(78);
expect(controller.get('seoTitle').toString()).to.equal(expected);
});
});
});
describe('seoDescription', function () {
it('should be the metaDescription if one exists', function () {
let controller = this.subject({
model: EmberObject.create({
metaDescription: 'a description'
2015-02-18 23:02:48 +03:00
})
});
expect(controller.get('seoDescription')).to.equal('a description');
});
it.skip('should be generated from the rendered markdown if not explicitly set', function () {
// can't test right now because the rendered markdown is being pulled
// from the DOM via jquery
});
it('should truncate to 156 characters with an appended ellipsis', function () {
let longDescription = new Array(200).join('a');
let controller = this.subject({
model: EmberObject.create()
2015-02-18 23:02:48 +03:00
});
expect(longDescription.length).to.equal(199);
run(function () {
let expected = `${longDescription.substr(0, 156)}…`;
2015-02-18 23:02:48 +03:00
controller.set('metaDescriptionScratch', longDescription);
expect(controller.get('seoDescription').toString().length).to.equal(164);
expect(controller.get('seoDescription').toString()).to.equal(expected);
});
});
});
describe('seoURL', function () {
it('should be the URL of the blog if no post slug exists', function () {
let controller = this.subject({
config: EmberObject.create({blogUrl: 'http://my-ghost-blog.com'}),
model: EmberObject.create()
2015-02-18 23:02:48 +03:00
});
expect(controller.get('seoURL')).to.equal('http://my-ghost-blog.com/');
});
it('should be the URL of the blog plus the post slug', function () {
let controller = this.subject({
config: EmberObject.create({blogUrl: 'http://my-ghost-blog.com'}),
model: EmberObject.create({slug: 'post-slug'})
2015-02-18 23:02:48 +03:00
});
expect(controller.get('seoURL')).to.equal('http://my-ghost-blog.com/post-slug/');
});
it('should update when the post slug changes', function () {
let controller = this.subject({
config: EmberObject.create({blogUrl: 'http://my-ghost-blog.com'}),
model: EmberObject.create({slug: 'post-slug'})
2015-02-18 23:02:48 +03:00
});
expect(controller.get('seoURL')).to.equal('http://my-ghost-blog.com/post-slug/');
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('model.slug', 'changed-slug');
expect(controller.get('seoURL')).to.equal('http://my-ghost-blog.com/changed-slug/');
});
});
it('should truncate a long URL to 70 characters with an appended ellipsis', function () {
let blogURL = 'http://my-ghost-blog.com';
let longSlug = new Array(75).join('a');
let controller = this.subject({
config: EmberObject.create({blogUrl: blogURL}),
model: EmberObject.create({slug: longSlug})
2015-02-18 23:02:48 +03:00
});
let expected;
expect(longSlug.length).to.equal(74);
2015-02-18 23:02:48 +03:00
expected = `${blogURL}/${longSlug}/`;
expected = `${expected.substr(0, 70)}…`;
2015-02-18 23:02:48 +03:00
expect(controller.get('seoURL').toString().length).to.equal(78);
expect(controller.get('seoURL').toString()).to.equal(expected);
});
});
describe('togglePage', function () {
it('should toggle the page property', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
page: false,
isNew: true
})
});
expect(controller.get('model.page')).to.not.be.ok;
run(function () {
2015-02-18 23:02:48 +03:00
controller.send('togglePage');
expect(controller.get('model.page')).to.be.ok;
});
});
it('should not save the post if it is still new', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
page: false,
isNew: true,
save() {
2015-02-18 23:02:48 +03:00
this.incrementProperty('saved');
return RSVP.resolve();
2015-02-18 23:02:48 +03:00
}
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.send('togglePage');
expect(controller.get('model.page')).to.be.ok;
expect(controller.get('model.saved')).to.not.be.ok;
});
});
it('should save the post if it is not new', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
page: false,
isNew: false,
save() {
2015-02-18 23:02:48 +03:00
this.incrementProperty('saved');
return RSVP.resolve();
2015-02-18 23:02:48 +03:00
}
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.send('togglePage');
expect(controller.get('model.page')).to.be.ok;
expect(controller.get('model.saved')).to.equal(1);
});
});
});
describe('toggleFeatured', function () {
it('should toggle the featured property', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
featured: false,
isNew: true
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.send('toggleFeatured');
expect(controller.get('model.featured')).to.be.ok;
});
});
it('should not save the post if it is still new', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
featured: false,
isNew: true,
save() {
2015-02-18 23:02:48 +03:00
this.incrementProperty('saved');
return RSVP.resolve();
2015-02-18 23:02:48 +03:00
}
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.send('toggleFeatured');
expect(controller.get('model.featured')).to.be.ok;
expect(controller.get('model.saved')).to.not.be.ok;
});
});
it('should save the post if it is not new', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
featured: false,
isNew: false,
save() {
2015-02-18 23:02:48 +03:00
this.incrementProperty('saved');
return RSVP.resolve();
2015-02-18 23:02:48 +03:00
}
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.send('toggleFeatured');
expect(controller.get('model.featured')).to.be.ok;
expect(controller.get('model.saved')).to.equal(1);
});
});
});
describe('updateSlug', function () {
it('should reset slugValue to the previous slug when the new slug is blank or unchanged', function () {
let controller = this.subject({
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'slug'
})
});
run(function () {
2015-02-18 23:02:48 +03:00
// unchanged
controller.set('slugValue', 'slug');
controller.send('updateSlug', controller.get('slugValue'));
expect(controller.get('model.slug')).to.equal('slug');
expect(controller.get('slugValue')).to.equal('slug');
});
run(function () {
2015-02-18 23:02:48 +03:00
// unchanged after trim
controller.set('slugValue', 'slug ');
controller.send('updateSlug', controller.get('slugValue'));
expect(controller.get('model.slug')).to.equal('slug');
expect(controller.get('slugValue')).to.equal('slug');
});
run(function () {
2015-02-18 23:02:48 +03:00
// blank
controller.set('slugValue', '');
controller.send('updateSlug', controller.get('slugValue'));
expect(controller.get('model.slug')).to.equal('slug');
expect(controller.get('slugValue')).to.equal('slug');
});
});
it('should not set a new slug if the server-generated slug matches existing slug', function (done) {
let controller = this.subject({
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str.split('#')[0]);
2015-02-18 23:02:48 +03:00
this.set('lastPromise', promise);
return promise;
}
}),
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'whatever'
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('slugValue', 'whatever#slug');
controller.send('updateSlug', controller.get('slugValue'));
RSVP.resolve(controller.get('lastPromise')).then(function () {
2015-02-18 23:02:48 +03:00
expect(controller.get('model.slug')).to.equal('whatever');
done();
}).catch(done);
});
});
it('should not set a new slug if the only change is to the appended increment value', function (done) {
let controller = this.subject({
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let sanitizedStr = str.replace(/[^a-zA-Z]/g, '');
let promise = RSVP.resolve(`${sanitizedStr}-2`);
2015-02-18 23:02:48 +03:00
this.set('lastPromise', promise);
return promise;
}
}),
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'whatever'
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('slugValue', 'whatever!');
controller.send('updateSlug', controller.get('slugValue'));
RSVP.resolve(controller.get('lastPromise')).then(function () {
2015-02-18 23:02:48 +03:00
expect(controller.get('model.slug')).to.equal('whatever');
done();
}).catch(done);
});
});
it('should set the slug if the new slug is different', function (done) {
let controller = this.subject({
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str);
2015-02-18 23:02:48 +03:00
this.set('lastPromise', promise);
return promise;
}
}),
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'whatever',
save: K
2015-02-18 23:02:48 +03:00
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('slugValue', 'changed');
controller.send('updateSlug', controller.get('slugValue'));
RSVP.resolve(controller.get('lastPromise')).then(function () {
2015-02-18 23:02:48 +03:00
expect(controller.get('model.slug')).to.equal('changed');
done();
}).catch(done);
});
});
it('should save the post when the slug changes and the post is not new', function (done) {
let controller = this.subject({
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str);
2015-02-18 23:02:48 +03:00
this.set('lastPromise', promise);
return promise;
}
}),
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'whatever',
saved: 0,
isNew: false,
save() {
2015-02-18 23:02:48 +03:00
this.incrementProperty('saved');
}
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('slugValue', 'changed');
controller.send('updateSlug', controller.get('slugValue'));
RSVP.resolve(controller.get('lastPromise')).then(function () {
2015-02-18 23:02:48 +03:00
expect(controller.get('model.slug')).to.equal('changed');
expect(controller.get('model.saved')).to.equal(1);
done();
}).catch(done);
});
});
it('should not save the post when the slug changes and the post is new', function (done) {
let controller = this.subject({
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str);
2015-02-18 23:02:48 +03:00
this.set('lastPromise', promise);
return promise;
}
}),
model: EmberObject.create({
2015-02-18 23:02:48 +03:00
slug: 'whatever',
saved: 0,
isNew: true,
save() {
2015-02-18 23:02:48 +03:00
this.incrementProperty('saved');
}
})
});
run(function () {
2015-02-18 23:02:48 +03:00
controller.set('slugValue', 'changed');
controller.send('updateSlug', controller.get('slugValue'));
RSVP.resolve(controller.get('lastPromise')).then(function () {
2015-02-18 23:02:48 +03:00
expect(controller.get('model.slug')).to.equal('changed');
expect(controller.get('model.saved')).to.equal(0);
done();
}).catch(done);
});
});
});
}
);