Skip to content

Commit

Permalink
Merge pull request #133 from gravity-ui/fix-close-with-timeout
Browse files Browse the repository at this point in the history
fix(PM): close with timeout
  • Loading branch information
iapolya authored Oct 11, 2024
2 parents edfd93b + 6ad7e8c commit 5c0e9f8
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 105 deletions.
5 changes: 3 additions & 2 deletions src/promo-manager/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ export class Controller {
this.stateActions.removeFromQueue(slug);

this.updateProgressInfo(slug);

this.triggerNextPromo();
};

cancelPromo = (slug: Nullable<PromoSlug>, closeActiveTimeout = 0) => {
Expand Down Expand Up @@ -628,12 +626,15 @@ export class Controller {

private closePromo = (slug: PromoSlug) => {
this.logger.debug('Close promo', slug);

if (!this.isActive(slug)) {
return;
}

this.stateActions.clearActive();
this.stateActions.removeFromQueue(slug);

this.emitChange();
this.triggerNextPromo();
};

Expand Down
103 changes: 0 additions & 103 deletions src/promo-manager/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,109 +232,6 @@ describe('repeated runs', function () {
});
});

describe('trigger subscribe', () => {
let controller: Controller;

beforeEach(() => {
controller = new Controller(testOptions);
});

it('request start -> 1 update', async () => {
const callback = jest.fn();

controller.subscribe(callback);

await controller.requestStart('boardPoll');

expect(callback).toHaveBeenCalledTimes(1);
});

it('finish -> 1 update', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.finishPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(1);
});

it('cancel -> 1 update', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.cancelPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(1);
});

it('skipPromo -> 1 update', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.skipPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(1);
});

it('start and finish -> 2 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

await controller.requestStart('boardPoll');
await controller.finishPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(2);
});

it('start, finish and has next -> 3 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

const promise1 = controller.requestStart('boardPoll');
const promise2 = controller.requestStart('ganttPoll');

await Promise.all([promise1, promise2]);

await controller.finishPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(3);
});

it('start and cancel -> 2 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

await controller.requestStart('boardPoll');
await controller.cancelPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(2);
});

it('double start and cancel -> 3 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

const promise1 = controller.requestStart('boardPoll');
const promise2 = controller.requestStart('ganttPoll');

await Promise.all([promise1, promise2]);

await controller.cancelPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(3);
});
});

describe('update last call info', () => {
let controller: Controller;
const promo = 'boardPoll';
Expand Down
149 changes: 149 additions & 0 deletions src/promo-manager/tests/subscribe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {Controller} from '../core/controller';
import {testOptions} from './options';
import {waitForNextTick} from './utils';

describe('trigger subscribe', () => {
let controller: Controller;

beforeEach(() => {
controller = new Controller(testOptions);
});

it('finish with timeout => update progress info, close active', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.finishPromo('boardPoll', 100);

expect(controller.state.base.activePromo).toBe('boardPoll');
expect(controller.state.progress?.finishedPromos.includes('boardPoll')).toBe(true);
expect(callback).toHaveBeenCalledTimes(1);

await waitForNextTick(100);

expect(controller.state.base.activePromo).toBe(null);
expect(callback).toHaveBeenCalledTimes(2);
});

it('cancel with timeout => update progress info, close active', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.cancelPromo('boardPoll', 100);

expect(controller.state.base.activePromo).toBe('boardPoll');
expect(controller.state.progress?.progressInfoByPromo['boardPoll']).toBeDefined();
expect(callback).toHaveBeenCalledTimes(1);

await waitForNextTick(100);

expect(controller.state.base.activePromo).toBe(null);
expect(callback).toHaveBeenCalledTimes(2);
});
});

describe('optimize subscribe', () => {
let controller: Controller;

beforeEach(() => {
controller = new Controller(testOptions);
});

it('request start -> 1 update', async () => {
const callback = jest.fn();

controller.subscribe(callback);

await controller.requestStart('boardPoll');

expect(callback).toHaveBeenCalledTimes(1);
});

it('finish -> 2 updates', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.finishPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(2);
});

it('cancel -> 2 updates', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.cancelPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(2);
});

it('skipPromo -> 1 update', async () => {
await controller.requestStart('boardPoll');

const callback = jest.fn();
controller.subscribe(callback);

await controller.skipPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(1);
});

it('start and finish -> 3 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

await controller.requestStart('boardPoll');
await controller.finishPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(3);
});

it('start, finish and has next -> 4 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

const promise1 = controller.requestStart('boardPoll');
const promise2 = controller.requestStart('ganttPoll');
await Promise.all([promise1, promise2]);

await controller.finishPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(4);
});

it('start and cancel -> 3 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

await controller.requestStart('boardPoll');
await controller.cancelPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(3);
});

it('double start and cancel -> 3 updates', async () => {
const callback = jest.fn();

controller.subscribe(callback);

const promise1 = controller.requestStart('boardPoll');
const promise2 = controller.requestStart('ganttPoll');

await Promise.all([promise1, promise2]);

await controller.cancelPromo('boardPoll');

expect(callback).toHaveBeenCalledTimes(4);
});
});

0 comments on commit 5c0e9f8

Please sign in to comment.