2018-01-17 16:27:37 +03:00
|
|
|
import EmberObject from '@ember/object';
|
|
|
|
import RSVP from 'rsvp';
|
2019-03-05 21:13:47 +03:00
|
|
|
import {defineProperty} from '@ember/object';
|
2018-01-17 16:27:37 +03:00
|
|
|
import {describe, it} from 'mocha';
|
|
|
|
import {expect} from 'chai';
|
2019-01-21 14:44:30 +03:00
|
|
|
import {settled} from '@ember/test-helpers';
|
2018-01-17 16:27:37 +03:00
|
|
|
import {setupTest} from 'ember-mocha';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
describe('Unit: Controller: editor', function () {
|
2019-01-21 14:44:30 +03:00
|
|
|
setupTest();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
describe('generateSlug', function () {
|
2019-01-21 14:44:30 +03:00
|
|
|
it('should generate a slug and set it on the post', async function () {
|
|
|
|
let controller = this.owner.lookup('controller:editor');
|
|
|
|
|
|
|
|
controller.set('slugGenerator', EmberObject.create({
|
|
|
|
generateSlug(slugType, str) {
|
|
|
|
return RSVP.resolve(`${str}-slug`);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
controller.set('post', EmberObject.create({slug: ''}));
|
|
|
|
|
|
|
|
controller.set('post.titleScratch', 'title');
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
expect(controller.get('post.slug')).to.equal('');
|
|
|
|
|
|
|
|
await controller.get('generateSlug').perform();
|
|
|
|
|
|
|
|
expect(controller.get('post.slug')).to.equal('title-slug');
|
2018-01-17 16:27:37 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
it('should not set the destination if the title is "(Untitled)" and the post already has a slug', async function () {
|
|
|
|
let controller = this.owner.lookup('controller:editor');
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
controller.set('slugGenerator', EmberObject.create({
|
|
|
|
generateSlug(slugType, str) {
|
|
|
|
return RSVP.resolve(`${str}-slug`);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
controller.set('post', EmberObject.create({slug: 'whatever'}));
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
expect(controller.get('post.slug')).to.equal('whatever');
|
|
|
|
|
|
|
|
controller.set('post.titleScratch', '(Untitled)');
|
2019-01-21 14:44:30 +03:00
|
|
|
await controller.get('generateSlug').perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(controller.get('post.slug')).to.equal('whatever');
|
2018-01-17 16:27:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('saveTitle', function () {
|
2019-01-21 14:44:30 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
this.controller = this.owner.lookup('controller:editor');
|
|
|
|
this.controller.set('target', {send() {}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should invoke generateSlug if the post is new and a title has not been set', async function () {
|
|
|
|
let {controller} = this;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
controller.set('target', {send() {}});
|
2019-03-05 21:13:47 +03:00
|
|
|
defineProperty(controller, 'generateSlug', task(function * () {
|
2019-01-21 14:44:30 +03:00
|
|
|
this.set('post.slug', 'test-slug');
|
|
|
|
yield RSVP.resolve();
|
|
|
|
}));
|
|
|
|
controller.set('post', EmberObject.create({isNew: true}));
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
expect(controller.get('post.isNew')).to.be.true;
|
|
|
|
expect(controller.get('post.titleScratch')).to.not.be.ok;
|
|
|
|
|
|
|
|
controller.set('post.titleScratch', 'test');
|
2019-01-21 14:44:30 +03:00
|
|
|
await controller.get('saveTitle').perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(controller.get('post.titleScratch')).to.equal('test');
|
|
|
|
expect(controller.get('post.slug')).to.equal('test-slug');
|
2018-01-17 16:27:37 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
it('should invoke generateSlug if the post is not new and it\'s title is "(Untitled)"', async function () {
|
|
|
|
let {controller} = this;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
controller.set('target', {send() {}});
|
2019-03-05 21:13:47 +03:00
|
|
|
defineProperty(controller, 'generateSlug', task(function * () {
|
2019-01-21 14:44:30 +03:00
|
|
|
this.set('post.slug', 'test-slug');
|
|
|
|
yield RSVP.resolve();
|
|
|
|
}));
|
|
|
|
controller.set('post', EmberObject.create({isNew: false, title: '(Untitled)'}));
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
expect(controller.get('post.isNew')).to.be.false;
|
|
|
|
expect(controller.get('post.titleScratch')).to.not.be.ok;
|
|
|
|
|
|
|
|
controller.set('post.titleScratch', 'New Title');
|
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
await controller.get('saveTitle').perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(controller.get('post.titleScratch')).to.equal('New Title');
|
|
|
|
expect(controller.get('post.slug')).to.equal('test-slug');
|
2018-01-17 16:27:37 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
it('should not invoke generateSlug if the post is new but has a title', async function () {
|
|
|
|
let {controller} = this;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
controller.set('target', {send() {}});
|
2019-03-05 21:13:47 +03:00
|
|
|
defineProperty(controller, 'generateSlug', task(function * () {
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(false, 'generateSlug should not be called').to.equal(true);
|
|
|
|
yield RSVP.resolve();
|
|
|
|
}));
|
|
|
|
controller.set('post', EmberObject.create({
|
|
|
|
isNew: true,
|
|
|
|
title: 'a title'
|
|
|
|
}));
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
expect(controller.get('post.isNew')).to.be.true;
|
|
|
|
expect(controller.get('post.title')).to.equal('a title');
|
|
|
|
expect(controller.get('post.titleScratch')).to.not.be.ok;
|
|
|
|
|
|
|
|
controller.set('post.titleScratch', 'test');
|
2019-01-21 14:44:30 +03:00
|
|
|
await controller.get('saveTitle').perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(controller.get('post.titleScratch')).to.equal('test');
|
|
|
|
expect(controller.get('post.slug')).to.not.be.ok;
|
2018-01-17 16:27:37 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
it('should not invoke generateSlug if the post is not new and the title is not "(Untitled)"', async function () {
|
|
|
|
let {controller} = this;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
controller.set('target', {send() {}});
|
2019-03-05 21:13:47 +03:00
|
|
|
defineProperty(controller, 'generateSlug', task(function * () {
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(false, 'generateSlug should not be called').to.equal(true);
|
|
|
|
yield RSVP.resolve();
|
|
|
|
}));
|
|
|
|
controller.set('post', EmberObject.create({isNew: false}));
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
expect(controller.get('post.isNew')).to.be.false;
|
|
|
|
expect(controller.get('post.title')).to.not.be.ok;
|
|
|
|
|
|
|
|
controller.set('post.titleScratch', 'title');
|
2019-01-21 14:44:30 +03:00
|
|
|
await controller.get('saveTitle').perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-01-21 14:44:30 +03:00
|
|
|
expect(controller.get('post.titleScratch')).to.equal('title');
|
|
|
|
expect(controller.get('post.slug')).to.not.be.ok;
|
2018-01-17 16:27:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|