diff --git a/tests/roles.js b/tests/roles.js new file mode 100644 index 0000000..814d28a --- /dev/null +++ b/tests/roles.js @@ -0,0 +1,18 @@ +import { Selector as $, Role } from 'testcafe' +import { addInstanceButton, getUrl, instanceInput } from './utils' + +function login(t, username, password) { + return t.typeText(instanceInput, 'localhost:3000') + .click(addInstanceButton) + .expect(getUrl()).eql('http://localhost:3000/auth/sign_in') + .typeText($('input#user_email'), username) + .typeText($('input#user_password'), password) + .click($('button[type=submit]')) + .expect(getUrl()).contains('/oauth/authorize') + .click($('button[type=submit]:not(.negative)')) + .expect(getUrl()).eql('http://localhost:4002/') +} + +export const foobarRole = Role('http://localhost:4002/settings/instances/add', async t => { + await login(t, 'foobar@localhost:3000', 'foobarfoobar') +}) \ No newline at end of file diff --git a/tests/spec/02-login-spec.js b/tests/spec/02-login-spec.js index 92b37a1..a118f2f 100644 --- a/tests/spec/02-login-spec.js +++ b/tests/spec/02-login-spec.js @@ -1,13 +1,15 @@ import { Selector as $ } from 'testcafe' import { addInstanceButton, getUrl, instanceInput, login, settingsButton } from '../utils' +import { foobarRole } from '../roles' fixture `02-login-spec.js` .page `http://localhost:4002` const formError = $('.form-error') -test('Cannot log in to a fake instance', async t => { +test('Cannot log in to a fake instance', async t => { await t.click($('a').withText('log in to an instance')) + .expect(getUrl()).contains('/settings/instances/add') .typeText(instanceInput, 'fake.nolanlawson.com') .click(addInstanceButton) .expect(formError.exists).ok() @@ -20,12 +22,12 @@ test('Cannot log in to a fake instance', async t => { }) test('Logs in to localhost:3000', async t => { - await login(t, 'foobar@localhost:3000', 'foobarfoobar') + await t.useRole(foobarRole) .expect($('article.status-article').exists).ok() }) test('Logs out', async t => { - await login(t, 'foobar@localhost:3000', 'foobarfoobar') + await t.useRole(foobarRole) .click(settingsButton) .click($('a').withText('Instances')) .click($('a').withText('localhost:3000')) diff --git a/tests/spec/03-basic-timeline-spec.js b/tests/spec/03-basic-timeline-spec.js index c79b2bf..d21c631 100644 --- a/tests/spec/03-basic-timeline-spec.js +++ b/tests/spec/03-basic-timeline-spec.js @@ -1,17 +1,15 @@ import { Selector as $ } from 'testcafe' import { getUrl, login, validateTimeline } from '../utils' import { homeTimeline, notifications, localTimeline, favorites } from '../fixtures' +import { foobarRole } from '../roles' fixture `03-basic-timeline-spec.js` .page `http://localhost:4002` - .beforeEach(async t => { - await login(t, 'foobar@localhost:3000', 'foobarfoobar') - }) const firstArticle = $('.virtual-list-item[aria-hidden=false] .status-article') test('Shows the home timeline', async t => { - await t + await t.useRole(foobarRole) .expect(firstArticle.hasAttribute('aria-setsize')).ok() .expect(firstArticle.getAttribute('aria-posinset')).eql('0') @@ -21,21 +19,24 @@ test('Shows the home timeline', async t => { }) test('Shows notifications', async t => { - await t.click($('nav a[aria-label=Notifications]')) + await t.useRole(foobarRole) + .click($('nav a[aria-label=Notifications]')) .expect(getUrl()).contains('/notifications') await validateTimeline(t, notifications) }) test('Shows the local timeline', async t => { - await t.click($('nav a[aria-label=Local]')) - await t.expect(getUrl()).contains('/local') + await t.useRole(foobarRole) + .click($('nav a[aria-label=Local]')) + .expect(getUrl()).contains('/local') await validateTimeline(t, localTimeline) }) test('Shows the federated timeline', async t => { - await t.click($('nav a[aria-label=Community]')) + await t.useRole(foobarRole) + .click($('nav a[aria-label=Community]')) .expect(getUrl()).contains('/community') .click($('a').withText('Federated')) .expect(getUrl()).contains('/federated') @@ -44,7 +45,8 @@ test('Shows the federated timeline', async t => { }) test('Shows favorites', async t => { - await t.click($('nav a[aria-label=Community]')) + await t.useRole(foobarRole) + .click($('nav a[aria-label=Community]')) .expect(getUrl()).contains('/community') .click($('a').withText('Favorites')) .expect(getUrl()).contains('/favorites') diff --git a/tests/utils.js b/tests/utils.js index 11bfb2d..112420e 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -6,20 +6,6 @@ export const addInstanceButton = $('.add-new-instance button') export const getUrl = exec(() => window.location.href) -export function login(t, username, password) { - return t.click($('a').withText('log in to an instance')) - .expect(getUrl()).contains('/settings/instances/add') - .typeText(instanceInput, 'localhost:3000') - .click(addInstanceButton) - .expect(getUrl()).eql('http://localhost:3000/auth/sign_in') - .typeText($('input#user_email'), username) - .typeText($('input#user_password'), password) - .click($('button[type=submit]')) - .expect(getUrl()).contains('/oauth/authorize') - .click($('button[type=submit]:not(.negative)')) - .expect(getUrl()).eql('http://localhost:4002/') -} - export function getNthVirtualArticle (n) { return $(`.virtual-list-item[aria-hidden="false"] article[aria-posinset="${n}"]`) }