Skip to content

Commit

Permalink
feat: New modal component to stop a running build (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
minghay authored Jul 31, 2024
1 parent 35c1225 commit 341bc9d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/components/pipeline/modal/stop-build/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';

export default class PipelineModalStopBuildComponent extends Component {
@service shuttle;

@tracked isDisabled = false;

@tracked errorMessage = null;

@tracked successMessage = null;

@action
async stopBuild() {
await this.shuttle
.fetchFromApi('put', `/builds/${this.args.buildId}`, {
status: 'ABORTED'
})
.then(() => {
this.successMessage = 'Build stopped successfully';
this.isDisabled = true;
})
.catch(err => {
this.errorMessage = err.message;
});
}
}
39 changes: 39 additions & 0 deletions app/components/pipeline/modal/stop-build/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<BsModal
id="stop-build-modal"
@onHide={{fn @closeModal}}
@onSubmit={{fn this.stopBuild}}
as |modal|
>
<modal.header>
<div class="modal-title">
Stop the build
</div>
</modal.header>
<modal.body>
{{#if this.errorMessage}}
<InfoMessage
@message={{this.errorMessage}}
@type="warning"
@icon="exclamation-triangle"
/>
{{/if}}
{{#if this.successMessage}}
<InfoMessage
@message={{this.successMessage}}
@type="success"
@icon="check-circle"
/>
{{/if}}
Are you sure you want to stop the build?
</modal.body>
<modal.footer>
<BsButton
id="stop-build"
@type="primary"
@onClick={{modal.submit}}
disabled={{this.isDisabled}}
>
Yes
</BsButton>
</modal.footer>
</BsModal>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'screwdriver-ui/tests/helpers';
import { click, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import sinon from 'sinon';

module('Integration | Component | pipeline/modal/stop-build', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
this.setProperties({
buildId: 1,
closeModal: () => {}
});

await render(
hbs`<Pipeline::Modal::StopBuild
@buildId={{this.buildId}}
@closeModal={{this.closeModal}}
/>`
);

assert.dom('.alert').doesNotExist();
assert.dom('#stop-build').exists({ count: 1 });
assert.dom('#stop-build').isEnabled();
});

test('it displays error message when stop fails', async function (assert) {
const shuttle = this.owner.lookup('service:shuttle');
const errorMessage = 'Failed to stop build';
const shuttleStub = sinon
.stub(shuttle, 'fetchFromApi')
.rejects({ message: errorMessage });

this.setProperties({
buildId: 1,
closeModal: () => {}
});

await render(
hbs`<Pipeline::Modal::StopBuild
@buildId={{this.buildId}}
@closeModal={{this.closeModal}}
/>`
);

await click('#stop-build');

assert.equal(shuttleStub.calledOnce, true);
assert.dom('.alert').exists({ count: 1 });
assert.dom('.alert > span').hasText(errorMessage);
});

test('it displays success message when stop succeeds', async function (assert) {
const shuttle = this.owner.lookup('service:shuttle');
const shuttleStub = sinon.stub(shuttle, 'fetchFromApi').resolves();

this.setProperties({
buildId: 1,
closeModal: () => {}
});

await render(
hbs`<Pipeline::Modal::StopBuild
@buildId={{this.buildId}}
@closeModal={{this.closeModal}}
/>`
);

await click('#stop-build');

assert.equal(shuttleStub.calledOnce, true);
assert.dom('.alert').exists({ count: 1 });
assert.dom('.alert > span').hasText('Build stopped successfully');
assert.dom('#stop-build').isDisabled();
});
});

0 comments on commit 341bc9d

Please sign in to comment.