2016-08-11 09:58:38 +03:00
|
|
|
/* jshint expr:true */
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2017-01-19 14:20:33 +03:00
|
|
|
import run from 'ember-runloop';
|
|
|
|
import wait from 'ember-test-helpers/wait';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {describe, it} from 'mocha';
|
|
|
|
import {expect} from 'chai';
|
|
|
|
import {setupComponentTest} from 'ember-mocha';
|
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
describe('Integration: Component: gh-task-button', function() {
|
|
|
|
setupComponentTest('gh-task-button', {
|
2016-08-11 09:58:38 +03:00
|
|
|
integration: true
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('renders', function () {
|
2017-03-07 20:28:52 +03:00
|
|
|
// sets button text using positional param
|
|
|
|
this.render(hbs`{{gh-task-button "Test"}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
expect(this.$('button')).to.exist;
|
|
|
|
expect(this.$('button')).to.contain('Test');
|
|
|
|
expect(this.$('button')).to.have.prop('disabled', false);
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
this.render(hbs`{{gh-task-button class="testing"}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
expect(this.$('button')).to.have.class('testing');
|
2017-03-07 20:28:52 +03:00
|
|
|
// default button text is "Save"
|
|
|
|
expect(this.$('button')).to.contain('Save');
|
2017-01-19 14:20:33 +03:00
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
// passes disabled attr
|
|
|
|
this.render(hbs`{{gh-task-button disabled=true buttonText="Test"}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
expect(this.$('button')).to.have.prop('disabled', true);
|
2017-03-07 20:28:52 +03:00
|
|
|
// allows button text to be set via hash param
|
|
|
|
expect(this.$('button')).to.contain('Test');
|
2017-01-19 14:20:33 +03:00
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
// passes type attr
|
|
|
|
this.render(hbs`{{gh-task-button type="submit"}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
expect(this.$('button')).to.have.attr('type', 'submit');
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
// passes tabindex attr
|
|
|
|
this.render(hbs`{{gh-task-button tabindex="-1"}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
expect(this.$('button')).to.have.attr('tabindex', '-1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows spinner whilst running', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
}));
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
2017-04-11 18:44:37 +03:00
|
|
|
expect(this.$('button')).to.have.descendants('svg');
|
2017-01-19 14:20:33 +03:00
|
|
|
}, 20);
|
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('appears disabled whilst running', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
}));
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
expect(this.$('button'), 'initial class').to.not.have.class('appear-disabled');
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button'), 'running class').to.have.class('appear-disabled');
|
|
|
|
}, 20);
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button'), 'ended class').to.not.have.class('appear-disabled');
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-01-19 14:20:33 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
it('shows success on success', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
return true;
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button')).to.have.class('gh-btn-green');
|
|
|
|
expect(this.$('button')).to.contain('Saved');
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-03-07 20:28:52 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
2017-03-10 00:48:54 +03:00
|
|
|
it('assigns specified success class on success', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
return true;
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-task-button task=myTask successClass="im-a-success"}}`);
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button')).to.not.have.class('gh-btn-green');
|
|
|
|
expect(this.$('button')).to.have.class('im-a-success');
|
|
|
|
expect(this.$('button')).to.contain('Saved');
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-03-10 00:48:54 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
it('shows failure when task errors', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
try {
|
|
|
|
yield timeout(50);
|
|
|
|
throw new ReferenceError('test error');
|
|
|
|
} catch (error) {
|
|
|
|
// noop, prevent mocha triggering unhandled error assert
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button')).to.have.class('gh-btn-red');
|
|
|
|
expect(this.$('button')).to.contain('Retry');
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-03-07 20:28:52 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows failure on falsy response', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button')).to.have.class('gh-btn-red');
|
|
|
|
expect(this.$('button')).to.contain('Retry');
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-03-07 20:28:52 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
2017-03-10 00:48:54 +03:00
|
|
|
it('assigns specified failure class on failure', function (done) {
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-task-button task=myTask failureClass="im-a-failure"}}`);
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('button')).to.not.have.class('gh-btn-red');
|
|
|
|
expect(this.$('button')).to.have.class('im-a-failure');
|
|
|
|
expect(this.$('button')).to.contain('Retry');
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-03-10 00:48:54 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
|
|
|
|
2017-01-19 14:20:33 +03:00
|
|
|
it('performs task on click', function (done) {
|
|
|
|
let taskCount = 0;
|
|
|
|
|
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
taskCount = taskCount + 1;
|
|
|
|
}));
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
this.$('button').click();
|
|
|
|
|
|
|
|
wait().then(() => {
|
|
|
|
expect(taskCount, 'taskCount').to.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
|
2017-04-11 18:44:37 +03:00
|
|
|
it.skip('keeps button size when showing spinner', function (done) {
|
2017-01-19 14:20:33 +03:00
|
|
|
this.set('myTask', task(function* () {
|
|
|
|
yield timeout(50);
|
|
|
|
}));
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
this.render(hbs`{{gh-task-button task=myTask}}`);
|
2017-01-19 14:20:33 +03:00
|
|
|
let width = this.$('button').width();
|
|
|
|
let height = this.$('button').height();
|
|
|
|
expect(this.$('button')).to.not.have.attr('style');
|
|
|
|
|
|
|
|
this.get('myTask').perform();
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
// we can't test exact width/height because Chrome/Firefox use different rounding methods
|
|
|
|
// expect(this.$('button')).to.have.attr('style', `width: ${width}px; height: ${height}px;`);
|
|
|
|
|
|
|
|
let [widthInt] = width.toString().split('.');
|
|
|
|
let [heightInt] = height.toString().split('.');
|
|
|
|
|
|
|
|
expect(this.$('button').attr('style')).to.have.string(`width: ${widthInt}`);
|
|
|
|
expect(this.$('button').attr('style')).to.have.string(`height: ${heightInt}`);
|
|
|
|
}, 20);
|
|
|
|
|
|
|
|
run.later(this, function () {
|
|
|
|
// chai-jquery test doesn't work because Firefox outputs blank string
|
|
|
|
// expect(this.$('button')).to.not.have.attr('style');
|
|
|
|
expect(this.$('button').attr('style')).to.be.blank;
|
2017-03-13 12:01:27 +03:00
|
|
|
}, 100);
|
2017-01-19 14:20:33 +03:00
|
|
|
|
|
|
|
wait().then(done);
|
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|