From 35475c2b920ac1ec8d526acb61795884f3241645 Mon Sep 17 00:00:00 2001
From: Matthew Dillon <mrdillon@alaska.edu>
Date: Tue, 10 Nov 2015 11:02:23 -0700
Subject: [PATCH] Rough in some tests

---
 app/mirage/config.js                          | 15 ++++
 app/mirage/factories/strains.js               | 17 +++++
 app/pods/protected/strains/index/template.hbs |  2 +-
 app/pods/protected/strains/show/template.hbs  |  2 +-
 .../strains/strain-form/template.hbs          |  4 +-
 tests/acceptance/strains-test.js              | 76 +++++++++++++++++++
 6 files changed, 112 insertions(+), 4 deletions(-)
 create mode 100644 app/mirage/factories/strains.js
 create mode 100644 tests/acceptance/strains-test.js

diff --git a/app/mirage/config.js b/app/mirage/config.js
index 3982c0c..e39ccee 100644
--- a/app/mirage/config.js
+++ b/app/mirage/config.js
@@ -24,4 +24,19 @@ export function testConfig() {
   this.post('/characteristics');
   this.get('/characteristics/:id');
   this.put('/characteristics/:id');
+
+  this.get('/strains', function(db /*, request*/) {
+    return {
+      strains: db.strains,
+      species: db.species,
+    };
+  });
+  this.post('/strains');
+  this.get('/strains/:id', function(db, request) {
+    return {
+      strain: db.strains.find(request.params.id),
+      species: db.species, // Just send back everything we've got
+    }
+  });
+  this.put('/strains/:id');
 }
diff --git a/app/mirage/factories/strains.js b/app/mirage/factories/strains.js
new file mode 100644
index 0000000..6165528
--- /dev/null
+++ b/app/mirage/factories/strains.js
@@ -0,0 +1,17 @@
+import Mirage, { faker } from 'ember-cli-mirage';
+
+export default Mirage.Factory.extend({
+  measurements: [],
+  characteristics: [],
+  species: 0,
+  strainName() { return faker.lorem.words().join(' '); },
+  typeStrain: faker.random.boolean(),
+  accessionNumbers() { return faker.lorem.words().join(' '); },
+  genbank() { return faker.lorem.words().join(' '); },
+  wholeGenomeSequence() { return faker.lorem.words().join(' '); },
+  isolatedFrom: faker.lorem.sentences(),
+  notes: faker.lorem.sentences(),
+  totalMeasurements: 0,
+  sortOrder: faker.random.number(),
+  canEdit: faker.random.boolean(),
+});
diff --git a/app/pods/protected/strains/index/template.hbs b/app/pods/protected/strains/index/template.hbs
index 077d22a..851f93b 100644
--- a/app/pods/protected/strains/index/template.hbs
+++ b/app/pods/protected/strains/index/template.hbs
@@ -1,5 +1,5 @@
 <h2>{{genus-name}} Strains</h2>
-<h3>Total strains: {{model.length}}</h3>
+<h3 id="total-strains">Total strains: {{model.length}}</h3>
 
 {{add-button label="Add Strain" link="protected.strains.new" canAdd=metaData.canAdd}}
 
diff --git a/app/pods/protected/strains/show/template.hbs b/app/pods/protected/strains/show/template.hbs
index 8a10e60..87d5d4f 100644
--- a/app/pods/protected/strains/show/template.hbs
+++ b/app/pods/protected/strains/show/template.hbs
@@ -1,7 +1,7 @@
 <div class="span-1">
   <fieldset class="flakes-information-box">
     <legend>
-      Strain {{model.strainNameMU}}
+      {{model.strainNameMU}}
     </legend>
 
     {{! ROW 1 }}
diff --git a/app/pods/protected/strains/strain-form/template.hbs b/app/pods/protected/strains/strain-form/template.hbs
index 109d037..fef122d 100644
--- a/app/pods/protected/strains/strain-form/template.hbs
+++ b/app/pods/protected/strains/strain-form/template.hbs
@@ -4,7 +4,7 @@
     <div data-row-span="2">
       <div data-field-span="1">
         <label>Strain Name</label>
-        {{input value=strain.strainName}}
+        {{input value=strain.strainName class="strain-name"}}
       </div>
       <div data-field-span="1">
         <label>Type Strain?</label>
@@ -62,7 +62,7 @@
     Cancel
   </a>
   {{#if strain.hasDirtyAttributes}}
-    <button type="submit" class="button-green smaller">
+    <button type="submit" class="button-green smaller save-strain">
       Save
     </button>
   {{/if}}
diff --git a/tests/acceptance/strains-test.js b/tests/acceptance/strains-test.js
new file mode 100644
index 0000000..a52e7ed
--- /dev/null
+++ b/tests/acceptance/strains-test.js
@@ -0,0 +1,76 @@
+import Ember from 'ember';
+import { module, test } from 'qunit';
+import startApp from '../helpers/start-app';
+import { authenticateSession } from '../helpers/ember-simple-auth';
+
+module('Acceptance | strains', {
+  beforeEach: function() {
+    this.application = startApp();
+    authenticateSession(this.application, {
+      access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJiYWN0ZGIiLCJzdWIiOiIxIiwiZXhwIjoxNDQ2NTAyMjI2LCJpYXQiOjE0NDY0OTg2MjZ9.vIjKHAsp2TkCV505EbtCo2xQT-2oQkB-Nv5y0b6E7Mg"
+    });
+    server.create('users', { role: 'A', canEdit: true });
+  },
+
+  afterEach: function() {
+    Ember.run(this.application, 'destroy');
+  }
+});
+
+test('visiting /strains', function(assert) {
+  const species = server.create('species');
+  const strains = server.createList('strains', 20, { species: species.id });
+  visit('/strains');
+
+  andThen(function() {
+    assert.equal(currentURL(), '/strains');
+    assert.equal(find(".flakes-table > tbody > tr").length, strains.length);
+    assert.equal(find("#total-strains").text(), "Total strains: 20");
+  });
+});
+
+test('visiting /strains/:id', function(assert) {
+  const species = server.create('species');
+  const strain = server.create('strains', { species: species.id });
+  visit(`/strains/${strain.id}`);
+
+  andThen(function() {
+    assert.equal(currentURL(), `/strains/${strain.id}`);
+    const typeStrain = strain.typeStrain ? 'T' : '';
+    assert.equal(find(".flakes-information-box > legend").text().trim(), `${strain.strainName}${typeStrain}`);
+  });
+});
+
+test('editing /strains/:id/edit', function(assert) {
+  const species = server.create('species');
+  const strain = server.create('strains', { canEdit: true , species: species.id });
+  visit(`/strains/${strain.id}/edit`);
+
+  andThen(function() {
+    assert.equal(currentURL(), `/strains/${strain.id}/edit`);
+
+    fillIn('.strain-name', 'Revised Strain Name');
+    click('.save-strain');
+
+    andThen(function() {
+      assert.equal(currentURL(), `/strains/${strain.id}`);
+      const typeStrain = strain.typeStrain ? 'T' : '';
+      assert.equal(find(".flakes-information-box > legend").text().trim(), `Revised Strain Name${typeStrain}`);
+    });
+  });
+});
+
+test('creating /strains/new', function(assert) {
+  visit(`/strains/new`);
+
+  andThen(function() {
+    assert.equal(currentURL(), `/strains/new`);
+    fillIn('.strain-name', 'New Strain Name');
+    click('.save-strain');
+
+    andThen(function() {
+      assert.equal(find(".flakes-information-box > legend").text().trim(), `New Strain Name`);
+      assert.equal(server.db.strains.length, 1);
+    });
+  });
+});