Ghost/ghost/admin/tests/acceptance/settings/unsplash-test.js
Naz d84a70752d Removed more leftover unsplash code & fixed tests
refs refs 07abb5443a

- Found some more unnecessary code for previous unsplash setting structure and removed it.
- Refactored the tests to be more in line to how AMP tests are done
2021-02-18 15:03:53 +13:00

133 lines
5.1 KiB
JavaScript

import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
import {authenticateSession, invalidateSession} from 'ember-simple-auth/test-support';
import {
beforeEach,
describe,
it
} from 'mocha';
import {click, currentURL, find, findAll, triggerEvent} from '@ember/test-helpers';
import {expect} from 'chai';
import {setupApplicationTest} from 'ember-mocha';
import {setupMirage} from 'ember-cli-mirage/test-support';
import {visit} from '../../helpers/visit';
describe('Acceptance: Settings - Integrations - Unsplash', function () {
let hooks = setupApplicationTest();
setupMirage(hooks);
it('redirects to signin when not authenticated', async function () {
await invalidateSession();
await visit('/integrations/unsplash');
expect(currentURL(), 'currentURL').to.equal('/signin');
});
it('redirects to staff page when authenticated as contributor', async function () {
let role = this.server.create('role', {name: 'Contributor'});
this.server.create('user', {roles: [role], slug: 'test-user'});
await authenticateSession();
await visit('/integrations/unsplash');
expect(currentURL(), 'currentURL').to.equal('/staff/test-user');
});
it('redirects to staff page when authenticated as author', async function () {
let role = this.server.create('role', {name: 'Author'});
this.server.create('user', {roles: [role], slug: 'test-user'});
await authenticateSession();
await visit('/integrations/unsplash');
expect(currentURL(), 'currentURL').to.equal('/staff/test-user');
});
it('redirects to staff page when authenticated as editor', async function () {
let role = this.server.create('role', {name: 'Editor'});
this.server.create('user', {roles: [role], slug: 'test-user'});
await authenticateSession();
await visit('/integrations/unsplash');
expect(currentURL(), 'currentURL').to.equal('/staff');
});
describe('when logged in', function () {
beforeEach(async function () {
let role = this.server.create('role', {name: 'Administrator'});
this.server.create('user', {roles: [role]});
return await authenticateSession();
});
it('it can activate/deactivate', async function () {
await visit('/integrations/unsplash');
// has correct url
expect(currentURL(), 'currentURL').to.equal('/integrations/unsplash');
// it's enabled by default when settings is empty
expect(find('[data-test-unsplash-checkbox]').checked, 'checked by default').to.be.true;
await click('[data-test-unsplash-checkbox]');
expect(find('[data-test-unsplash-checkbox]').checked, 'unsplash checkbox').to.be.false;
// trigger a save
await click('[data-test-save-button]');
// server should now have an unsplash setting
let [lastRequest] = this.server.pretender.handledRequests.slice(-1);
let params = JSON.parse(lastRequest.requestBody);
expect(params.settings.findBy('key', 'unsplash').value).to.equal(false);
// save via CMD-S shortcut
await click('[data-test-unsplash-checkbox]');
await triggerEvent('.gh-app', 'keydown', {
keyCode: 83, // s
metaKey: ctrlOrCmd === 'command',
ctrlKey: ctrlOrCmd === 'ctrl'
});
// we've already saved in this test so there's no on-screen indication
// that we've had another save, check the request was fired instead
let [newRequest] = this.server.pretender.handledRequests.slice(-1);
params = JSON.parse(newRequest.requestBody);
expect(find('[data-test-unsplash-checkbox]').checked, 'AMP checkbox').to.be.true;
expect(params.settings.findBy('key', 'unsplash').value).to.equal(true);
});
it('warns when leaving without saving', async function () {
await visit('/integrations/unsplash');
// has correct url
expect(currentURL(), 'currentURL').to.equal('/integrations/unsplash');
// AMP is enabled by default
expect(find('[data-test-unsplash-checkbox]').checked, 'AMP checkbox default').to.be.true;
await click('[data-test-unsplash-checkbox]');
expect(find('[data-test-unsplash-checkbox]').checked, 'Unsplash checkbox').to.be.false;
await visit('/settings/labs');
expect(findAll('.fullscreen-modal').length, 'unsaved changes modal exists').to.equal(1);
// Leave without saving
await click('.fullscreen-modal [data-test-leave-button]');
expect(currentURL(), 'currentURL after leave without saving').to.equal('/settings/labs');
await visit('/integrations/unsplash');
expect(currentURL(), 'currentURL').to.equal('/integrations/unsplash');
// settings were not saved
expect(find('[data-test-unsplash-checkbox]').checked, 'Unsplash checkbox').to.be.true;
});
});
});