From 6e25bc33cf3de2198e6a033f3d37e11d581d9fe9 Mon Sep 17 00:00:00 2001
From: Jennifer Spencer <jennifer.spencer@yld.io>
Date: Tue, 1 May 2018 10:18:15 +0100
Subject: [PATCH] test: abstract authentication for e2e tests into helper file

---
 test/.eslintrc                       |  3 +++
 test/helpers/authenticate-fixture.js | 25 ++++++++++++++++++++++
 test/submission.e2e.js               | 31 +++++-----------------------
 3 files changed, 33 insertions(+), 26 deletions(-)
 create mode 100644 test/helpers/authenticate-fixture.js

diff --git a/test/.eslintrc b/test/.eslintrc
index 922c66c..302bdda 100644
--- a/test/.eslintrc
+++ b/test/.eslintrc
@@ -6,5 +6,8 @@
   "globals": {
     "fixture": true,
     "test": true
+  },
+  "rules": {
+    "no-param-reassign": ["error", { "props": false }]
   }
 }
diff --git a/test/helpers/authenticate-fixture.js b/test/helpers/authenticate-fixture.js
new file mode 100644
index 0000000..384143d
--- /dev/null
+++ b/test/helpers/authenticate-fixture.js
@@ -0,0 +1,25 @@
+import { ClientFunction } from 'testcafe'
+import { addUser } from '@pubsweet/db-manager'
+import authentication from 'pubsweet-server/src/authentication'
+import { startServer, setup, teardown } from './setup'
+
+const admin = {
+  username: 'tester',
+  email: 'tester@example.com',
+  password: 'password',
+  orcid: '0000-0001',
+  admin: true,
+}
+
+export default chosenFixture =>
+  chosenFixture
+    .before(startServer)
+    .beforeEach(async t => {
+      await setup()
+      const user = await addUser(admin)
+      t.ctx.token = authentication.token.create(user)
+      t.ctx.localStorageSet = ClientFunction(token =>
+        localStorage.setItem('token', token),
+      )
+    })
+    .afterEach(teardown)
diff --git a/test/submission.e2e.js b/test/submission.e2e.js
index e27bcc7..287593b 100644
--- a/test/submission.e2e.js
+++ b/test/submission.e2e.js
@@ -1,38 +1,17 @@
 import replay from 'replay'
-import { Selector, ClientFunction } from 'testcafe'
-import { addUser } from '@pubsweet/db-manager'
-import authentication from 'pubsweet-server/src/authentication'
-import { startServer, setup, teardown } from './helpers/setup'
+import { Selector } from 'testcafe'
 import { dashboard } from './pageObjects'
+import authenticateFixture from './helpers/authenticate-fixture'
 
 replay.fixtures = `${__dirname}/http-mocks`
 
-const admin = {
-  username: 'tester',
-  email: 'tester@example.com',
-  password: 'password',
-  orcid: '0000-0001',
-  admin: true,
-}
-let token
-
-fixture('Submission')
-  .before(startServer)
-  .beforeEach(async () => {
-    await setup()
-    const user = await addUser(admin)
-    token = authentication.token.create(user)
-  })
-  .afterEach(teardown)
-
-const localStorageSet = ClientFunction((key, val) =>
-  localStorage.setItem(key, val),
-)
+const f = fixture('Submission')
+authenticateFixture(f)
 
 test('Happy path', async t => {
   // fake login by navigating to site and injecting token into local storage
   await t.navigateTo(dashboard.url)
-  await localStorageSet('token', token)
+  await t.ctx.localStorageSet(t.ctx.token)
 
   await t.navigateTo(dashboard.url).click('[data-test-id=submit]')
 
-- 
GitLab