Files

249 lines
7.8 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
import Feedbin from 'dashtab/components/settings/integrations/feedbin-form';
function registerServices(owner, overrides = {}) {
const http = overrides.http ?? { request: async () => ({}) };
const storage = overrides.storage ?? { set: async () => {} };
const sites = overrides.sites ?? { reload: async () => {} };
const notifications = overrides.notifications ?? { startPolling: () => {} };
owner.register('service:http', http, { instantiate: false });
owner.register('service:storage', storage, { instantiate: false });
owner.register('service:sites', sites, { instantiate: false });
owner.register('service:notifications', notifications, {
instantiate: false,
});
}
module(
'Integration | Component | settings/integrations/feedbin-form',
function (hooks) {
setupRenderingTest(hooks);
test('it renders the credential form', async function (assert) {
registerServices(this.owner);
const noop = () => {};
await render(<template><Feedbin @close={{noop}} /></template>);
assert.dom().containsText('Add Feedbin Integration');
assert.dom('#feedbin-url').exists().hasValue('https://feedbin.com');
assert.dom('#feedbin-email').exists();
assert.dom('#feedbin-password').exists();
assert.dom('form').exists();
});
test('submitting validates and stores the credentials', async function (assert) {
const requests = [];
const saved = [];
let reloaded = false;
let polling = false;
let closed = false;
registerServices(this.owner, {
http: {
request: async (url, options) => {
requests.push({ url, options });
return {};
},
},
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
sites: {
reload: async () => {
reloaded = true;
},
},
notifications: {
startPolling: () => {
polling = true;
},
},
});
const closeDialog = () => {
closed = true;
};
await render(<template><Feedbin @close={{closeDialog}} /></template>);
await fillIn('#feedbin-email', 'me@example.com');
await fillIn('#feedbin-password', 'secret');
await triggerEvent('form', 'submit');
assert.strictEqual(requests.length, 1);
assert.strictEqual(
requests[0].url,
'https://api.feedbin.com/v2/authentication.json'
);
assert.strictEqual(
requests[0].options.headers['Authorization'],
`Basic ${btoa('me@example.com:secret')}`
);
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].namespace, 'integrations');
assert.strictEqual(saved[0].key, 'feedbin');
assert.strictEqual(saved[0].value.type, 'feedbin');
assert.strictEqual(saved[0].value.status, 'active');
assert.strictEqual(saved[0].value.url, 'https://feedbin.com');
assert.strictEqual(saved[0].value.email, 'me@example.com');
assert.strictEqual(saved[0].value.token, btoa('me@example.com:secret'));
assert.true(reloaded);
assert.true(polling);
assert.true(closed);
});
test('submitting a self-hosted URL derives the api subdomain', async function (assert) {
const requests = [];
const saved = [];
registerServices(this.owner, {
http: {
request: async (url, options) => {
requests.push({ url, options });
return {};
},
},
storage: {
set: async (namespace, key, value) => saved.push(value),
},
});
const noop = () => {};
await render(<template><Feedbin @close={{noop}} /></template>);
await fillIn('#feedbin-url', 'feedbin.example.com');
await fillIn('#feedbin-email', 'me@example.com');
await fillIn('#feedbin-password', 'secret');
await triggerEvent('form', 'submit');
assert.strictEqual(
requests[0].url,
'https://api.feedbin.example.com/v2/authentication.json'
);
assert.strictEqual(saved[0].url, 'https://feedbin.example.com');
});
test('it rejects an invalid server URL', async function (assert) {
const requests = [];
registerServices(this.owner, {
http: {
request: async (...args) => requests.push(args),
},
});
const noop = () => {};
await render(<template><Feedbin @close={{noop}} /></template>);
await fillIn('#feedbin-url', 'not a valid url');
await fillIn('#feedbin-email', 'me@example.com');
await fillIn('#feedbin-password', 'secret');
await triggerEvent('form', 'submit');
assert.dom('#feedbin-url').hasValue('not a valid url');
assert.dom().containsText('Please enter a valid Feedbin URL');
assert.strictEqual(requests.length, 0);
});
test('it reports invalid credentials without storing them', async function (assert) {
const saved = [];
let closed = false;
registerServices(this.owner, {
http: {
request: async () => {
throw { status: 401 };
},
},
storage: {
set: async (...args) => saved.push(args),
},
});
const closeDialog = () => {
closed = true;
};
await render(<template><Feedbin @close={{closeDialog}} /></template>);
await fillIn('#feedbin-email', 'me@example.com');
await fillIn('#feedbin-password', 'wrong');
await triggerEvent('form', 'submit');
assert.dom().containsText('Invalid email or password');
assert.strictEqual(saved.length, 0);
assert.false(closed);
});
test('it requires an email and password', async function (assert) {
registerServices(this.owner);
const noop = () => {};
await render(<template><Feedbin @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
assert.dom().containsText('Please enter your Feedbin email');
});
test('edit mode prefills the email and keeps the stored token when the password is blank', async function (assert) {
const requests = [];
const saved = [];
registerServices(this.owner, {
http: {
request: async (url, options) => {
requests.push({ url, options });
return {};
},
},
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
});
this.props = {
mode: 'edit',
integration: {
url: 'https://feedbin.example.com',
email: 'old@example.com',
token: 'OLDT0KEN',
colorClass: 'bg-emerald-600',
},
};
const noop = () => {};
await render(
<template><Feedbin @props={{this.props}} @close={{noop}} /></template>
);
assert.dom().containsText('Edit Feedbin');
assert.dom('#feedbin-url').hasValue('https://feedbin.example.com');
assert.dom('#feedbin-email').hasValue('old@example.com');
await triggerEvent('form', 'submit');
assert.strictEqual(requests.length, 1);
assert.strictEqual(
requests[0].url,
'https://api.feedbin.example.com/v2/authentication.json'
);
assert.strictEqual(
requests[0].options.headers['Authorization'],
'Basic OLDT0KEN'
);
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].value.url, 'https://feedbin.example.com');
assert.strictEqual(saved[0].value.email, 'old@example.com');
assert.strictEqual(saved[0].value.token, 'OLDT0KEN');
assert.strictEqual(saved[0].value.colorClass, undefined);
});
}
);