Ghost/ghost/admin/tests/integration/services/feature-test.js
Kevin Ansfield 73daa80b7f Migrate to latest ember, ember-mocha and modern ember testing (#1044)
no issue
- upgrade to latest `ember-source` and related dependencies including `ember-cli`
- upgrade to latest `ember-mocha` and modern ember testing setup
    - https://github.com/emberjs/rfcs/blob/master/text/0268-acceptance-testing-refactor.md
    - switch from using global acceptance test helpers and `native-dom-helpers` to using the new `ember-test-helpers` methods
    - use [`chai-dom`](https://github.com/nathanboktae/chai-dom) assertions where in some places (still a lot of places in the tests that could use these)
- pin `ember-in-viewport` to 3.0.x to work around incompatibilities between different versions used in `ember-light-table`, `ember-infinity`, and `ember-sticky-element`
    - incompatibilities manifested as "Invalid value used as weak map key" errors thrown when using `ember-light-table` (subscribers screen)
- pin `ember-power-datepicker` to unreleased version that contains a move from global acceptance test helpers to modern test helpers
2019-01-02 09:58:55 +00:00

324 lines
9.2 KiB
JavaScript

import EmberError from '@ember/error';
import FeatureService, {feature} from 'ghost-admin/services/feature';
import Pretender from 'pretender';
import wait from 'ember-test-helpers/wait';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {run} from '@ember/runloop';
import {setupTest} from 'ember-mocha';
function stubSettings(server, labs, validSave = true) {
let settings = [
{
id: '1',
type: 'blog',
key: 'labs',
value: JSON.stringify(labs)
}
];
server.get('/ghost/api/v2/admin/settings/', function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings})];
});
server.put('/ghost/api/v2/admin/settings/', function (request) {
let statusCode = (validSave) ? 200 : 400;
let response = (validSave) ? request.requestBody : JSON.stringify({
errors: [{
message: 'Test Error'
}]
});
return [statusCode, {'Content-Type': 'application/json'}, response];
});
}
function stubUser(server, accessibility, validSave = true) {
let users = [{
id: '1',
// Add extra properties for the validations
name: 'Test User',
email: 'test@example.com',
accessibility: JSON.stringify(accessibility),
roles: [{
id: 1,
name: 'Owner',
description: 'Owner'
}]
}];
server.get('/ghost/api/v2/admin/users/me/', function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify({users})];
});
server.put('/ghost/api/v2/admin/users/1/', function (request) {
let statusCode = (validSave) ? 200 : 400;
let response = (validSave) ? request.requestBody : JSON.stringify({
errors: [{
message: 'Test Error'
}]
});
return [statusCode, {'Content-Type': 'application/json'}, response];
});
}
function addTestFlag() {
FeatureService.reopen({
testFlag: feature('testFlag'),
testUserFlag: feature('testUserFlag', {user: true})
});
}
describe('Integration: Service: feature', function () {
setupTest('service:feature', {
integration: true
});
let server;
beforeEach(function () {
server = new Pretender();
});
afterEach(function () {
server.shutdown();
});
it('loads labs and user settings correctly', function () {
stubSettings(server, {testFlag: true});
stubUser(server, {testUserFlag: true});
addTestFlag();
let service = this.subject();
return service.fetch().then(() => {
expect(service.get('testFlag')).to.be.true;
expect(service.get('testUserFlag')).to.be.true;
});
});
it('returns false for set flag with config false and labs false', function () {
stubSettings(server, {testFlag: false});
stubUser(server, {});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', false);
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.false;
expect(service.get('testFlag')).to.be.false;
});
});
it('returns true for set flag with config true and labs false', function () {
stubSettings(server, {testFlag: false});
stubUser(server, {});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', true);
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.false;
expect(service.get('testFlag')).to.be.true;
});
});
it('returns true for set flag with config false and labs true', function () {
stubSettings(server, {testFlag: true});
stubUser(server, {});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', false);
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.true;
expect(service.get('testFlag')).to.be.true;
});
});
it('returns true for set flag with config true and labs true', function () {
stubSettings(server, {testFlag: true});
stubUser(server, {});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', true);
return service.fetch().then(() => {
expect(service.get('labs.testFlag')).to.be.true;
expect(service.get('testFlag')).to.be.true;
});
});
it('returns false for set flag with accessibility false', function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: false});
addTestFlag();
let service = this.subject();
return service.fetch().then(() => {
expect(service.get('accessibility.testUserFlag')).to.be.false;
expect(service.get('testUserFlag')).to.be.false;
});
});
it('returns true for set flag with accessibility true', function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: true});
addTestFlag();
let service = this.subject();
return service.fetch().then(() => {
expect(service.get('accessibility.testUserFlag')).to.be.true;
expect(service.get('testUserFlag')).to.be.true;
});
});
it('saves labs setting correctly', function () {
stubSettings(server, {testFlag: false});
stubUser(server, {testUserFlag: false});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', false);
return service.fetch().then(() => {
expect(service.get('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;
});
});
});
it('saves accessibility setting correctly', function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: false});
addTestFlag();
let service = this.subject();
return service.fetch().then(() => {
expect(service.get('testUserFlag')).to.be.false;
run(() => {
service.set('testUserFlag', true);
});
return wait().then(() => {
expect(server.handlers[3].numberOfCalls).to.equal(1);
expect(service.get('testUserFlag')).to.be.true;
});
});
});
it('notifies for server errors on labs save', function () {
stubSettings(server, {testFlag: false}, false);
stubUser(server, {});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', false);
return service.fetch().then(() => {
expect(service.get('testFlag')).to.be.false;
run(() => {
service.set('testFlag', true);
});
return wait().then(() => {
expect(
server.handlers[1].numberOfCalls,
'PUT call is made'
).to.equal(1);
expect(
service.get('notifications.alerts').length,
'number of alerts shown'
).to.equal(1);
expect(service.get('testFlag')).to.be.false;
});
});
});
it('notifies for server errors on accessibility save', function () {
stubSettings(server, {});
stubUser(server, {testUserFlag: false}, false);
addTestFlag();
let service = this.subject();
return service.fetch().then(() => {
expect(service.get('testUserFlag')).to.be.false;
run(() => {
service.set('testUserFlag', true);
});
return wait().then(() => {
expect(
server.handlers[3].numberOfCalls,
'PUT call is made'
).to.equal(1);
expect(
service.get('notifications.alerts').length,
'number of alerts shown'
).to.equal(1);
expect(service.get('testUserFlag')).to.be.false;
});
});
});
it('notifies for validation errors', function () {
stubSettings(server, {testFlag: false}, true, false);
stubUser(server, {});
addTestFlag();
let service = this.subject();
service.get('config').set('testFlag', false);
return service.fetch().then(() => {
expect(service.get('testFlag')).to.be.false;
run(() => {
expect(() => {
service.set('testFlag', true);
}, EmberError, 'threw validation error');
});
return wait().then(() => {
// ensure validation is happening before the API is hit
expect(server.handlers[1].numberOfCalls).to.equal(0);
expect(service.get('testFlag')).to.be.false;
});
});
});
});