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

591 lines
21 KiB
JavaScript
Raw Normal View History

/* eslint-disable camelcase */
import EmberObject from '@ember/object';
import RSVP from 'rsvp';
import boundOneWay from 'ghost-admin/utils/bound-one-way';
2016-11-24 01:50:57 +03:00
import {describe, it} from 'mocha';
import {run} from '@ember/runloop';
import {setupComponentTest} from 'ember-mocha';
2015-02-18 23:02:48 +03:00
function K() {
return this;
}
// TODO: convert to integration tests
describe.skip('Unit: Component: post-settings-menu', function () {
setupComponentTest('gh-post-settings-menu', {
needs: ['service:notifications', 'service:slug-generator', 'service:settings']
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
it('slugValue is one-way bound to post.slug', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'a-slug'
})
});
expect(component.get('post.slug')).to.equal('a-slug');
expect(component.get('slugValue')).to.equal('a-slug');
2016-11-24 01:50:57 +03:00
run(function () {
component.set('post.slug', 'changed-slug');
2016-11-24 01:50:57 +03:00
expect(component.get('slugValue')).to.equal('changed-slug');
2016-11-24 01:50:57 +03:00
});
run(function () {
component.set('slugValue', 'changed-directly');
2016-11-24 01:50:57 +03:00
expect(component.get('post.slug')).to.equal('changed-slug');
expect(component.get('slugValue')).to.equal('changed-directly');
2016-11-24 01:50:57 +03:00
});
run(function () {
// test that the one-way binding is still in place
component.set('post.slug', 'should-update');
2016-11-24 01:50:57 +03:00
expect(component.get('slugValue')).to.equal('should-update');
2016-11-24 01:50:57 +03:00
});
});
it('metaTitleScratch is one-way bound to post.metaTitle', function () {
let component = this.subject({
post: EmberObject.extend({
metaTitle: 'a title',
metaTitleScratch: boundOneWay('metaTitle')
}).create()
2016-11-24 01:50:57 +03:00
});
expect(component.get('post.metaTitle')).to.equal('a title');
expect(component.get('metaTitleScratch')).to.equal('a title');
2016-11-24 01:50:57 +03:00
run(function () {
component.set('post.metaTitle', 'a different title');
2016-11-24 01:50:57 +03:00
expect(component.get('metaTitleScratch')).to.equal('a different title');
2016-11-24 01:50:57 +03:00
});
run(function () {
component.set('metaTitleScratch', 'changed directly');
2016-11-24 01:50:57 +03:00
expect(component.get('post.metaTitle')).to.equal('a different title');
expect(component.get('post.metaTitleScratch')).to.equal('changed directly');
2016-11-24 01:50:57 +03:00
});
run(function () {
// test that the one-way binding is still in place
component.set('post.metaTitle', 'should update');
2016-11-24 01:50:57 +03:00
expect(component.get('metaTitleScratch')).to.equal('should update');
2016-11-24 01:50:57 +03:00
});
});
it('metaDescriptionScratch is one-way bound to post.metaDescription', function () {
let component = this.subject({
post: EmberObject.extend({
metaDescription: 'a description',
metaDescriptionScratch: boundOneWay('metaDescription')
}).create()
2016-11-24 01:50:57 +03:00
});
expect(component.get('post.metaDescription')).to.equal('a description');
expect(component.get('metaDescriptionScratch')).to.equal('a description');
2016-11-24 01:50:57 +03:00
run(function () {
component.set('post.metaDescription', 'a different description');
2016-11-24 01:50:57 +03:00
expect(component.get('metaDescriptionScratch')).to.equal('a different description');
2016-11-24 01:50:57 +03:00
});
run(function () {
component.set('metaDescriptionScratch', 'changed directly');
2016-11-24 01:50:57 +03:00
expect(component.get('post.metaDescription')).to.equal('a different description');
expect(component.get('metaDescriptionScratch')).to.equal('changed directly');
2016-11-24 01:50:57 +03:00
});
run(function () {
// test that the one-way binding is still in place
component.set('post.metaDescription', 'should update');
2016-11-24 01:50:57 +03:00
expect(component.get('metaDescriptionScratch')).to.equal('should update');
2016-11-24 01:50:57 +03:00
});
});
describe('seoTitle', function () {
it('should be the metaTitle if one exists', function () {
let component = this.subject({
post: EmberObject.extend({
titleScratch: 'should not be used',
2016-11-24 01:50:57 +03:00
metaTitle: 'a meta-title',
metaTitleScratch: boundOneWay('metaTitle')
}).create()
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoTitle')).to.equal('a meta-title');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should default to the title if an explicit meta-title does not exist', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
titleScratch: 'should be the meta-title'
})
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoTitle')).to.equal('should be the meta-title');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should be the metaTitle if both title and metaTitle exist', function () {
let component = this.subject({
post: EmberObject.extend({
titleScratch: 'a title',
2016-11-24 01:50:57 +03:00
metaTitle: 'a meta-title',
metaTitleScratch: boundOneWay('metaTitle')
}).create()
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoTitle')).to.equal('a meta-title');
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
it('should revert to the title if explicit metaTitle is removed', function () {
let component = this.subject({
post: EmberObject.extend({
titleScratch: 'a title',
2016-11-24 01:50:57 +03:00
metaTitle: 'a meta-title',
metaTitleScratch: boundOneWay('metaTitle')
}).create()
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoTitle')).to.equal('a meta-title');
2015-02-18 23:02:48 +03:00
run(function () {
component.set('post.metaTitle', '');
2015-02-18 23:02:48 +03:00
expect(component.get('seoTitle')).to.equal('a title');
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should truncate to 70 characters with an appended ellipsis', function () {
let longTitle = new Array(100).join('a');
let component = this.subject({
post: EmberObject.create()
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
expect(longTitle.length).to.equal(99);
run(function () {
2016-11-24 01:50:57 +03:00
let expected = `${longTitle.substr(0, 70)}…`;
2015-02-18 23:02:48 +03:00
component.set('metaTitleScratch', longTitle);
2016-11-24 01:50:57 +03:00
expect(component.get('seoTitle').toString().length).to.equal(78);
expect(component.get('seoTitle').toString()).to.equal(expected);
2015-02-18 23:02:48 +03:00
});
});
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
describe('seoDescription', function () {
it('should be the metaDescription if one exists', function () {
let component = this.subject({
post: EmberObject.extend({
metaDescription: 'a description',
metaDescriptionScratch: boundOneWay('metaDescription')
}).create()
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoDescription')).to.equal('a description');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
it('should be generated from the rendered mobiledoc if not explicitly set', function () {
let component = this.subject({
post: EmberObject.extend({
metaDescription: null,
metaDescriptionScratch: boundOneWay('metaDescription'),
author: RSVP.resolve(),
init() {
this._super(...arguments);
this.scratch = {
cards: [
['markdown-card', {
markdown: '# This is a <strong>test</strong> <script>foo</script>'
}]
]
};
}
}).create()
});
expect(component.get('seoDescription')).to.equal('This is a test');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should truncate to 156 characters with an appended ellipsis', function () {
let longDescription = new Array(200).join('a');
let component = this.subject({
post: EmberObject.create()
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
expect(longDescription.length).to.equal(199);
2015-02-18 23:02:48 +03:00
run(function () {
2016-11-24 01:50:57 +03:00
let expected = `${longDescription.substr(0, 156)}&hellip;`;
component.set('metaDescriptionScratch', longDescription);
2015-02-18 23:02:48 +03:00
expect(component.get('seoDescription').toString().length).to.equal(164);
expect(component.get('seoDescription').toString()).to.equal(expected);
2015-02-18 23:02:48 +03:00
});
});
2016-11-24 01:50:57 +03:00
});
describe('seoURL', function () {
it('should be the URL of the blog if no post slug exists', function () {
let component = this.subject({
2016-11-24 01:50:57 +03:00
config: EmberObject.create({blogUrl: 'http://my-ghost-blog.com'}),
post: EmberObject.create()
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
expect(component.get('seoURL')).to.equal('http://my-ghost-blog.com/');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should be the URL of the blog plus the post slug', function () {
let component = this.subject({
2016-11-24 01:50:57 +03:00
config: EmberObject.create({blogUrl: 'http://my-ghost-blog.com'}),
post: EmberObject.create({slug: 'post-slug'})
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoURL')).to.equal('http://my-ghost-blog.com/post-slug/');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should update when the post slug changes', function () {
let component = this.subject({
2016-11-24 01:50:57 +03:00
config: EmberObject.create({blogUrl: 'http://my-ghost-blog.com'}),
post: EmberObject.create({slug: 'post-slug'})
2015-02-18 23:02:48 +03:00
});
expect(component.get('seoURL')).to.equal('http://my-ghost-blog.com/post-slug/');
2016-11-24 01:50:57 +03:00
run(function () {
component.set('post.slug', 'changed-slug');
2016-11-24 01:50:57 +03:00
expect(component.get('seoURL')).to.equal('http://my-ghost-blog.com/changed-slug/');
2016-11-24 01:50:57 +03:00
});
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
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 component = this.subject({
2016-11-24 01:50:57 +03:00
config: EmberObject.create({blogUrl: blogURL}),
post: EmberObject.create({slug: longSlug})
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
let expected;
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
expect(longSlug.length).to.equal(74);
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
expected = `${blogURL}/${longSlug}/`;
expected = `${expected.substr(0, 70)}&hellip;`;
2015-02-18 23:02:48 +03:00
expect(component.get('seoURL').toString().length).to.equal(78);
expect(component.get('seoURL').toString()).to.equal(expected);
2016-11-24 01:50:57 +03:00
});
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
describe('togglePage', function () {
it('should toggle the page property', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
page: false,
isNew: true
})
2015-02-18 23:02:48 +03:00
});
expect(component.get('post.page')).to.not.be.ok;
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
run(function () {
component.send('togglePage');
expect(component.get('post.page')).to.be.ok;
2016-11-24 01:50:57 +03:00
});
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should not save the post if it is still new', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
page: false,
isNew: true,
save() {
this.incrementProperty('saved');
return RSVP.resolve();
}
})
});
run(function () {
component.send('togglePage');
2015-02-18 23:02:48 +03:00
expect(component.get('post.page')).to.be.ok;
expect(component.get('post.saved')).to.not.be.ok;
2015-02-18 23:02:48 +03:00
});
});
2016-11-24 01:50:57 +03:00
it('should save the post if it is not new', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
page: false,
isNew: false,
save() {
this.incrementProperty('saved');
return RSVP.resolve();
}
})
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
run(function () {
component.send('togglePage');
2016-11-24 01:50:57 +03:00
expect(component.get('post.page')).to.be.ok;
expect(component.get('post.saved')).to.equal(1);
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
});
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
describe('toggleFeatured', function () {
it('should toggle the featured property', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
featured: false,
isNew: true
})
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
run(function () {
component.send('toggleFeatured');
2015-02-18 23:02:48 +03:00
expect(component.get('post.featured')).to.be.ok;
2016-11-24 01:50:57 +03:00
});
});
2016-11-24 01:50:57 +03:00
it('should not save the post if it is still new', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
featured: false,
isNew: true,
save() {
this.incrementProperty('saved');
return RSVP.resolve();
}
})
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
run(function () {
component.send('toggleFeatured');
2015-02-18 23:02:48 +03:00
expect(component.get('post.featured')).to.be.ok;
expect(component.get('post.saved')).to.not.be.ok;
2015-02-18 23:02:48 +03:00
});
});
2016-11-24 01:50:57 +03:00
it('should save the post if it is not new', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
featured: false,
isNew: false,
save() {
this.incrementProperty('saved');
return RSVP.resolve();
}
})
});
run(function () {
component.send('toggleFeatured');
2016-11-24 01:50:57 +03:00
expect(component.get('post.featured')).to.be.ok;
expect(component.get('post.saved')).to.equal(1);
2016-11-24 01:50:57 +03:00
});
});
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
describe('updateSlug', function () {
it('should reset slugValue to the previous slug when the new slug is blank or unchanged', function () {
let component = this.subject({
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'slug'
})
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
run(function () {
// unchanged
component.set('slugValue', 'slug');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
expect(component.get('post.slug')).to.equal('slug');
expect(component.get('slugValue')).to.equal('slug');
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
run(function () {
// unchanged after trim
component.set('slugValue', 'slug ');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
expect(component.get('post.slug')).to.equal('slug');
expect(component.get('slugValue')).to.equal('slug');
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
run(function () {
// blank
component.set('slugValue', '');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
expect(component.get('post.slug')).to.equal('slug');
expect(component.get('slugValue')).to.equal('slug');
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should not set a new slug if the server-generated slug matches existing slug', function (done) {
let component = this.subject({
2016-11-24 01:50:57 +03:00
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str.split('#')[0]);
this.set('lastPromise', promise);
return promise;
}
}),
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'whatever'
})
});
2016-11-24 01:50:57 +03:00
run(function () {
component.set('slugValue', 'whatever#slug');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
RSVP.resolve(component.get('lastPromise')).then(function () {
expect(component.get('post.slug')).to.equal('whatever');
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
done();
}).catch(done);
2015-02-18 23:02:48 +03:00
});
});
2016-11-24 01:50:57 +03:00
it('should not set a new slug if the only change is to the appended increment value', function (done) {
let component = this.subject({
2016-11-24 01:50:57 +03:00
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let sanitizedStr = str.replace(/[^a-zA-Z]/g, '');
let promise = RSVP.resolve(`${sanitizedStr}-2`);
this.set('lastPromise', promise);
return promise;
}
}),
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'whatever'
})
});
run(function () {
component.set('slugValue', 'whatever!');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
RSVP.resolve(component.get('lastPromise')).then(function () {
expect(component.get('post.slug')).to.equal('whatever');
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
done();
}).catch(done);
});
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should set the slug if the new slug is different', function (done) {
let component = this.subject({
2016-11-24 01:50:57 +03:00
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str);
this.set('lastPromise', promise);
return promise;
}
}),
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'whatever',
save: K
})
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
run(function () {
component.set('slugValue', 'changed');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
RSVP.resolve(component.get('lastPromise')).then(function () {
expect(component.get('post.slug')).to.equal('changed');
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
done();
}).catch(done);
2015-02-18 23:02:48 +03:00
});
2016-11-24 01:50:57 +03:00
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
it('should save the post when the slug changes and the post is not new', function (done) {
let component = this.subject({
2016-11-24 01:50:57 +03:00
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str);
this.set('lastPromise', promise);
return promise;
}
}),
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'whatever',
saved: 0,
isNew: false,
save() {
this.incrementProperty('saved');
}
})
});
2015-02-18 23:02:48 +03:00
2016-11-24 01:50:57 +03:00
run(function () {
component.set('slugValue', 'changed');
component.send('updateSlug', component.get('slugValue'));
2015-02-18 23:02:48 +03:00
RSVP.resolve(component.get('lastPromise')).then(function () {
expect(component.get('post.slug')).to.equal('changed');
expect(component.get('post.saved')).to.equal(1);
2016-11-24 01:50:57 +03:00
done();
}).catch(done);
});
});
it('should not save the post when the slug changes and the post is new', function (done) {
let component = this.subject({
2016-11-24 01:50:57 +03:00
slugGenerator: EmberObject.create({
generateSlug(slugType, str) {
let promise = RSVP.resolve(str);
this.set('lastPromise', promise);
return promise;
}
}),
post: EmberObject.create({
2016-11-24 01:50:57 +03:00
slug: 'whatever',
saved: 0,
isNew: true,
save() {
this.incrementProperty('saved');
}
})
});
run(function () {
component.set('slugValue', 'changed');
component.send('updateSlug', component.get('slugValue'));
2016-11-24 01:50:57 +03:00
RSVP.resolve(component.get('lastPromise')).then(function () {
expect(component.get('post.slug')).to.equal('changed');
expect(component.get('post.saved')).to.equal(0);
2016-11-24 01:50:57 +03:00
done();
}).catch(done);
});
});
});
});