Skip to content

Commit

Permalink
Guarantee idempotence of CountdownElement#start
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 15, 2022
1 parent 8492b80 commit 4258680
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/javascript/packages/countdown-element/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,17 @@ describe('CountdownElement', () => {

expect(element.textContent).to.equal('0 minutes and 2 seconds');
});

describe('#start', () => {
it('is idempotent', () => {
const element = createElement({ startImmediately: 'false' });

sinon.spy(element, 'setTimeRemaining');

element.start();
element.start();

expect(clock.countTimers()).to.equal(1);
});
});
});
1 change: 1 addition & 0 deletions app/javascript/packages/countdown-element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class CountdownElement extends HTMLElement {
}

start(): void {
this.stop();
this.setTimeRemaining();
this.#pollIntervalId = window.setInterval(() => this.setTimeRemaining(), this.updateInterval);
}
Expand Down

0 comments on commit 4258680

Please sign in to comment.