Skip to content

Commit

Permalink
Finish test suite, add in beta pending button
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerhutchings committed Mar 7, 2017
1 parent 4eaca01 commit 5f42316
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 18 deletions.
6 changes: 3 additions & 3 deletions app/pages/lab/apply-for-beta-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class ApplyForBetaForm extends React.Component {
.catch(error => console.error('Error requesting project data', error))
.then(results => {
this.setState({ doingAsyncValidation: false });
if (results.every(result => typeof result === 'boolean' && result === true)) {
// if (results.every(result => typeof result === 'boolean' && result === true)) {
return true;
}
// }
const errors = results.filter(result => typeof result !== 'boolean');
return Promise.reject(errors);
})
Expand Down Expand Up @@ -170,7 +170,7 @@ class ApplyForBetaForm extends React.Component {
return (
<div>
<p className="form-help">The following errors need to be fixed:</p>
<ul className="form-help">
<ul className="form-help error-messages">
{errors.map(error => <li key={error}>{error}</li>)}
</ul>
</div>
Expand Down
151 changes: 142 additions & 9 deletions app/pages/lab/apply-for-beta-form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe('ApplyForBeta component:', function() {
let wrapper = Function.prototype;
let label = Function.prototype;
let checkbox = Function.prototype;

let mockPages = [];
let mockSubjectSets = [];

const testSetup = function() {
project = createProjectProp();
workflows = [1, 2, 3].map(i => ({
Expand All @@ -49,13 +51,31 @@ describe('ApplyForBeta component:', function() {
},
}));
applyFn = sinon.spy();
mockPages = [];
mockSubjectSets = [
{ id: "0", set_member_subjects_count: 98 },
];
};

beforeEach(function() {
testSetup();
ApplyForBetaForm.__Rewire__('apiClient', {
type: (type) => ({
get: () => {
let result = null;
const resolver = (value) => new Promise(resolve => resolve(value));
if (type === 'projects') {
result = {
get: () => resolver(mockPages),
};
} else if (type === 'subject_sets') {
result = resolver(mockSubjectSets);
}
return result;
},
}),
});

it('should render without crashing', function() {
testSetup();
shallow(<ApplyForBetaForm project={project} />);
});

Expand Down Expand Up @@ -145,7 +165,7 @@ describe('ApplyForBeta component:', function() {
assertCheckboxDisabled(checkbox);
});

xit('should be unchecked if the project has no active workflows', function() {
it('should be unchecked if the project has no active workflows', function() {
setActiveWorkflowStatusWrappers();
assertCheckboxChecked(checkbox, false);
});
Expand Down Expand Up @@ -216,14 +236,127 @@ describe('ApplyForBeta component:', function() {
});

describe('async validation:', function() {
it('should show an error if required pages don\'t exist');
it('should show an error if there aren\'t enough active subjects');
const testForErrorMessage = (regex, result, condition, done) => {
workflows[0].active = true;
wrapper = mount(<ApplyForBetaForm project={project} workflows={workflows} />);
wrapper.find('input[type="checkbox"]').forEach(checkbox => {
checkbox.simulate('change', { target: { checked: true }});
});
wrapper.find('button.standard-button').first().simulate('click');
setTimeout(() => {
const errorMessages = wrapper.find('ul.form-help').at(1).text();
const containsError = regex.test(errorMessages);
assert.ok(containsError === result, condition);
done();
}, 100)
};

describe('content page checks:', function () {
REQUIRED_PAGES.map(function (pageTitle) {
beforeEach(testSetup);

const pattern = 'The following pages are missing content:(.+?)' + pageTitle;
const regex = new RegExp(pattern);

it(`should show an error if ${pageTitle} doesn't exist`, function(done) {
testForErrorMessage(regex, true, `${pageTitle} page doesn't exist`, done);
});

it(`should show an error if ${pageTitle} doesn't have any content`, function(done) {
mockPages.push({
title: pageTitle,
content: '',
});
testForErrorMessage(regex, true, `${pageTitle} page doesn't contain any content`, done);
});

it(`shouldn't show an error if ${pageTitle} exists and has content`, function(done) {
mockPages.push({
title: pageTitle,
content: 'foobar',
});
testForErrorMessage(regex, false, `${pageTitle} page doesn't contain any content`, done);
});
});
});

describe('subject set checks:', function () {
beforeEach(testSetup);

const pattern = 'The project only has (\\d+?) of ' + MINIMUM_SUBJECT_COUNT + ' required subjects';
const regex = new RegExp(pattern);

it(`should show an error if there are less than ${MINIMUM_SUBJECT_COUNT} subjects`, function(done) {
testForErrorMessage(regex, true, `Project contains less than ${MINIMUM_SUBJECT_COUNT} subjects`, done);
});

it(`shouldn't show an error if there are ${MINIMUM_SUBJECT_COUNT} subjects`, function(done) {
mockSubjectSets.push({
id: "1",
set_member_subjects_count: 98,
});
testForErrorMessage(regex, false, `Project contains ${MINIMUM_SUBJECT_COUNT} subjects`, done);
});

it(`shouldn't show an error if there are more than ${MINIMUM_SUBJECT_COUNT} subjects`, function(done) {
mockSubjectSets.push({
id: "1",
set_member_subjects_count: 98,
}, {
id: "2",
set_member_subjects_count: 1,
});
testForErrorMessage(regex, false, `Project contains ${MINIMUM_SUBJECT_COUNT} subjects`, done);
});
});
});

describe('apply button behaviour:', function() {
it('should exist');
it('should be disabled unless all checkboxes are checked');
it('should call the applyFn prop on click');
beforeEach(testSetup);

it('should exist', function() {
wrapper = shallow(<ApplyForBetaForm project={project} workflows={workflows} />);
const button = wrapper.find('button.standard-button').first();
assert(button.length > 0, 'Button exists');
});

it('should be disabled unless all checkboxes are checked', function() {
workflows[0].active = true;
wrapper = mount(<ApplyForBetaForm project={project} workflows={workflows} />);
const checkboxes = wrapper.find('input[type="checkbox"]');
checkboxes.forEach(function(checkbox, index) {
if (index < (checkboxes.length - 1))
checkbox.simulate('change', { target: { checked: true }});
})
const button = wrapper.find('button.standard-button').first();
assert(button.prop('disabled') === true, 'Button is disabled');
});

it('should call the applyFn prop on click if all validations pass', function(done) {
workflows[0].active = true;
mockPages.push({
title: 'Research',
content: 'foobar',
}, {
title: 'FAQ',
content: 'foobar',
});
mockSubjectSets.push({
id: "1",
set_member_subjects_count: 2,
});

wrapper = mount(<ApplyForBetaForm project={project} workflows={workflows} applyFn={applyFn} />);
wrapper.find('input[type="checkbox"]').forEach(function(checkbox) {
checkbox.simulate('change', { target: { checked: true }});
})
wrapper.find('button.standard-button').first().simulate('click');

setTimeout(function() {
assert.ok(applyFn.calledOnce, 'applyFn has been called');
done();
}, 100);
});
});

});
8 changes: 2 additions & 6 deletions app/pages/lab/visibility.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ module.exports = React.createClass

setRadio: (property, value) ->
@set property, value
unless @isReviewable()
@setState
labPolicyReviewed: false
bestPracticesReviewed: false
feedbackReviewed: false

toggleCheckbox: (checkbox) ->
change = { }
Expand Down Expand Up @@ -134,8 +129,9 @@ module.exports = React.createClass
<span>Beta Approval Status: </span>
<span className="color-label orange">Pending</span>
</div>
<span>Review status has been applied for. <button type="button" disabled={@state.setting.beta_requested} onClick={@set.bind this, 'beta_requested', false}>Cancel application</button></span>
else
<ApplyForBetaForm project={@props.project} workflows={@state.workflows} />
<ApplyForBetaForm project={@props.project} workflows={@state.workflows} applyFn={@set.bind this, 'beta_requested', true} />
}

</div>
Expand Down

0 comments on commit 5f42316

Please sign in to comment.