Skip to content

Commit

Permalink
Merge pull request #293 from CenterForOpenScience/feature/tests
Browse files Browse the repository at this point in the history
[EOSF-594] Add example controller unit tests and component integration tests
  • Loading branch information
brianjgeiger authored Apr 28, 2017
2 parents 3d31e4b + 842875d commit 65b3d66
Show file tree
Hide file tree
Showing 9 changed files with 1,238 additions and 1,026 deletions.
4 changes: 2 additions & 2 deletions app/controllers/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ export default Ember.Controller.extend(Analytics, BasicsValidations, NodeActions
this.set('basicsLicense', {
licenseType: license || this.get('availableLicenses').toArray()[0],
year: this.get('model.licenseRecord') ? this.get('model.licenseRecord').year : date.getUTCFullYear().toString(),
copyrightHolders: this.get('model.licenseRecord') ? this.get('model.licenseRecord').copyright_holders.join(',') : ''
copyrightHolders: this.get('model.licenseRecord') ? this.get('model.licenseRecord').copyright_holders.join(', ') : ''
});
});
},
Expand Down Expand Up @@ -725,7 +725,7 @@ export default Ember.Controller.extend(Analytics, BasicsValidations, NodeActions
const currentNodeLicenseType = node.get('license');
const currentNodeLicenseRecord = node.get('nodeLicense');
const copyrightHolders = this.get('basicsLicense.copyrightHolders')
.split(',')
.split(', ')
.map(item => item.trim());

if (this.get('abstractChanged'))
Expand Down
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"toastr": "^2.1.2",
"hint.css": "^2.3.2",
"jquery.tagsinput": "^1.3.6",
"loaders.css": "^0.1.2"
"loaders.css": "^0.1.2",
"jquery-mockjax": "~2.2.1",
"Faker": "~3.0.0"
},
"resolutions": {
"jquery": "~2.2.4"
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
"ember-cli-code-coverage": "0.3.11",
"ember-cli-dependency-checker": "^1.2.0",
"ember-cli-dropzonejs": "0.7.0",
"ember-cli-fake-server": "0.3.2",
"ember-cli-eslint": "3.0.3",
"ember-cli-htmlbars": "^1.0.3",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-inject-live-reload": "^1.4.0",
"ember-cli-inline-content": "0.4.0",
"ember-cli-meta-tags": "3.0.1",
"ember-cli-postcss": "^3.0.1",
"ember-cli-qunit": "^2.0.0",
"ember-cli-qunit": "^3.0.0",
"ember-cli-release": "^0.2.9",
"ember-cli-sass": "5.3.1",
"ember-cli-sri": "^2.1.0",
Expand All @@ -56,7 +57,9 @@
"ember-collection": "1.0.0-alpha.6",
"ember-cp-validations": "^2.9.4",
"ember-data": "^2.7.0",
"ember-data-factory-guy": "2.11.6",
"ember-export-application-global": "^1.0.5",
"ember-faker": "^1.1.1",
"ember-font-awesome": "2.1.1",
"ember-get-config": "0.0.4",
"ember-i18n": "4.3.1",
Expand All @@ -73,6 +76,8 @@
"ember-truth-helpers": "1.2.0",
"ember-welcome-page": "^1.0.1",
"eslint": "^3.18.0",
"faker": "^4.1.0",
"jscs": "^3.0.7",
"liquid-fire": "0.24.1",
"loader.js": "^4.0.1",
"pagination-pager": "2.4.2",
Expand Down
39 changes: 32 additions & 7 deletions tests/helpers/module-for-acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,47 @@ import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
import FactoryGuy, { manualSetup } from 'ember-data-factory-guy';
import config from 'ember-get-config';
import FakeServer, { stubRequest } from 'ember-cli-fake-server';

const { RSVP: { Promise } } = Ember;

export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
this.application = startApp();
FakeServer.start();
manualSetup(this.application.__container__);
const url = config.OSF.apiUrl;
const provider = FactoryGuy.build('preprint-provider');

if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
stubRequest('get', url + '/v2/users/me', (request) => {
request.unauthorized({});
});
stubRequest('get', url + '/v2/preprint_providers', (request) => {
request.ok({data: [{
attributes: provider.data.attributes,
type: "preprint_providers",
id: "osf"
}]});
});
stubRequest('get', url + '/v2/preprint_providers/osf', (request) => {
request.ok({data: {
attributes: provider.data.attributes,
type: "preprint_providers",
id: "osf"
}});
});

if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => destroyApp(this.application));
FakeServer.stop();
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => destroyApp(this.application));
}
});
}
37 changes: 37 additions & 0 deletions tests/integration/components/author-link-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Ember from 'ember';
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import FactoryGuy, { manualSetup } from 'ember-data-factory-guy';

moduleForComponent('author-link', 'Integration | Component | author link', {
integration: true,
beforeEach: function() {
manualSetup(this.container);
}
});

test('renders non-links', function(assert) {
let contributorModel = FactoryGuy.make('contributor');
// Problem here is that author link expects a share search-result contributor,
// not a store instance of a contributor and its user(s).
let contributor = {users: {identifiers: []}};
contributor.users.name = contributorModel.get('users.fullName');
contributor = Ember.merge(contributor, contributorModel.serialize().data.attributes);
this.set('contributor', contributor);
this.render(hbs`{{author-link contributor=contributor}}`);
assert.ok(!this.$('a').length, 'Found a link when user has no identifiers');
assert.equal(this.$().text().trim(), contributorModel.get('users.fullName'), 'Text of author-link not equal to fullName of user');
});

test('renders links', function(assert) {
let contributorModel = FactoryGuy.make('contributor');
let contributor = {users: {identifiers: []}};
contributor.users.name = contributorModel.get('users.fullName');
contributor = Ember.merge(contributor, contributorModel.serialize().data.attributes);
contributor.users.identifiers.push('https://staging.osf.io/cool');
this.set('contributor', contributor);

this.render(hbs`{{author-link contributor=contributor}}`);
assert.ok(this.$('a').length, 'Expected a link in author-link as user has an identifier');
assert.equal(this.$().text().trim(), contributorModel.get('users.fullName'), 'Text of author-link not equal to fullName of user');
});
81 changes: 40 additions & 41 deletions tests/integration/components/supplementary-file-browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,51 @@ import hbs from 'htmlbars-inline-precompile';
import Ember from 'ember';

moduleForComponent('supplementary-file-browser', 'Integration | Component | supplementary file browser', {
integration: true
integration: true,
beforeEach: function() {
let providerFiles = () => Ember.RSVP.resolve(Ember.ArrayProxy.create({
content: Ember.A(),
meta: {
pagination: {
total: 1
}
}}));
let providersQuery = Ember.RSVP.resolve(Ember.A([{
name: 'osfstorage',
query: providerFiles
}]));

let node = Ember.Object.create({
dateModified: '10-11-2016',
title:'My Preprint Title',
files: providersQuery
});

let file = Ember.Object.create({

});
let preprint = Ember.Object.create({
primaryFile: file,
node: node,
provider: 'osf',
files: providersQuery
});

this.set('preprint', preprint);
this.set('node', node);
}
});

test('it renders', function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });

// Construct a fake preprint object to satisfy browser needs
let providerFiles = () => Ember.RSVP.resolve(Ember.ArrayProxy.create({
content: Ember.A(),
meta: {
pagination: {
total: 1
}
}}));
let providersQuery = Ember.RSVP.resolve(Ember.A([{
name: 'osfstorage',
query: providerFiles
}]));

let node = Ember.Object.create({
dateModified: '10-11-2016',
title:'My Preprint Title',
files: providersQuery
});

let file = Ember.Object.create({

});
let preprint = Ember.Object.create({
primaryFile: file,
node: node,
provider: 'osf',
files: providersQuery
});



this.set('preprint', preprint);
this.set('node', node);

this.render(hbs`{{supplementary-file-browser node=node preprint=preprint}}`);

assert.equal(this.$().text().trim().replace(/\s/g, ""), 'DownloadpreprintVersion:');

// this.on('changeFile', function() {});
});

// {{supplementary-file-browser preprint=model projectURL=model.links.html chooseFile=(action 'chooseFile')}}
test('fileDownloadURL computed property', function (assert) {
this.render(hbs`{{supplementary-file-browser node=node preprint=preprint}}`);

let url = this.$('.supplemental-downloads > a').attr('href')
assert.ok(url);
assert.ok(url.indexOf(this.get('primaryFile.guid')) !== -1, 'Url does not have file\'s guid in it');
});
53 changes: 1 addition & 52 deletions tests/unit/controllers/content/index-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {moduleFor, test} from 'ember-qunit';
import Ember from 'ember';
import { moduleFor, test } from 'ember-qunit';
import config from 'ember-get-config';

moduleFor('controller:content/index', 'Unit | Controller | content/index', {
Expand Down Expand Up @@ -268,57 +268,6 @@ test('fullLicenseText computed property', function (assert) {
});
});

test('fileDownloadURL computed property', function (assert) {
// Note: testing of the file download url string value should be done in the file-download-path util test
this.inject.service('store');

const store = this.store;
const ctrl = this.subject();

// Without a node should be null
Ember.run(() => {
const primaryFile = store.createRecord('file', {
guid: 'abc12',
path: '/test_path'
});

const model = store.createRecord('preprint', {
primaryFile
});

ctrl.setProperties({
model
});

ctrl.get('fileDownloadURL')
.then(url => assert.notOk(url));
});

// With file and node
Ember.run(() => {
const primaryFile = store.createRecord('file', {
guid: 'abc12',
path: '/test_path'
});

const model = store.createRecord('preprint', {
primaryFile
});

const node = store.createRecord('node', {
id: 'def34'
});

ctrl.setProperties({
model,
node
});

ctrl.get('fileDownloadURL')
.then(url => assert.ok(url));
});
});

test('hasShortenedDescription computed property', function (assert) {
this.inject.service('store');

Expand Down
39 changes: 39 additions & 0 deletions tests/unit/controllers/submit-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { moduleFor, test, skip } from 'ember-qunit';
import Ember from 'ember';
import wait from 'ember-test-helpers/wait';

const panelNames = [
'Discipline',
Expand Down Expand Up @@ -516,6 +517,44 @@ test('licenseChanged with no model set', function(assert) {
});
});

test('basicsLicense with multiple copyrightHolders', function(assert) {
const ctrl = this.subject();
const model = {
licenseRecord: {
copyright_holders: ['Frank', 'Everest']
}
};
Ember.run(() => {
ctrl.set('model', model);
assert.equal(ctrl.get('basicsLicense').copyrightHolders, 'Frank, Everest');
});
});

test('discardBasics properly joins copyrightHolders', function(assert) {
assert.expect(1);
const ctrl = this.subject();
this.inject.service('store');
const store = this.store;
Ember.run(() => {
const node = store.createRecord('node', {
tags: ['tags']
});
const model = store.createRecord('preprint', {
licenseRecord: {
copyright_holders: ['Frank', 'Everest']
}
});
const license = store.createRecord('license', {
'name': 'No license'
});
model.set('license', license);
ctrl.set('node', node);
ctrl.set('model', model);
ctrl.send('discardBasics');
return wait().then(() => assert.equal(ctrl.get('basicsLicense').copyrightHolders, 'Frank, Everest'));
});
})

test('basicsChanged computed property', function(assert) {
const ctrl = this.subject();
ctrl.set('tagsChanged', false);
Expand Down
Loading

0 comments on commit 65b3d66

Please sign in to comment.