-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(esl-event-listener): fix re-subscription when condition is used
Note: warnings for incorrect subscription is missing
- Loading branch information
Showing
4 changed files
with
115 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 108 additions & 23 deletions
131
src/modules/esl-event-listener/test/listener.resubscribe.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,119 @@ | ||
import {ESLEventUtils} from '../core/api'; | ||
|
||
|
||
describe('ESLEventUtils.subscribe resubscribing event', () => { | ||
const $host = document.createElement('div'); | ||
const handle = jest.fn(); | ||
const targ1 = document.createElement('button'); | ||
const targ2 = document.createElement('button'); | ||
const target = jest.fn(() => targ1); | ||
|
||
const listeners1 = ESLEventUtils.subscribe($host, {event: 'click', target}, handle); | ||
test('ESLEventUtils.subscribe subscription correct first time', () => { | ||
expect(listeners1.length).toBe(1); | ||
expect(ESLEventUtils.listeners($host).length).toBe(1); | ||
}); | ||
const $el1 = document.createElement('button'); | ||
const $el2 = document.createElement('button'); | ||
|
||
target.mockImplementation(() => targ2); | ||
const listeners2 = ESLEventUtils.subscribe($host, {event: 'click', target}, handle); | ||
test('ESLEventUtils.subscribe subscription correct second time', () => { | ||
expect(listeners2.length).toBe(1); | ||
expect(ESLEventUtils.listeners($host).length).toBe(1); | ||
}); | ||
describe('ESLEventUtils.subscribe dynamic resubscribtion of the same event with different target', () => { | ||
const handle = jest.fn(); | ||
const target = jest.fn(() => $el1); | ||
|
||
const listeners1 = ESLEventUtils.subscribe($host, {event: 'click', target}, handle); | ||
test('ESLEventUtils.subscribe subscription correct first time', () => { | ||
expect(listeners1.length).toBe(1); | ||
expect(ESLEventUtils.listeners($host)).toEqual(listeners1); | ||
}); | ||
|
||
// Change target | ||
target.mockImplementation(() => $el2); | ||
|
||
// Subscription descriptor is the same as produced in listener 1 situation | ||
const listeners2 = ESLEventUtils.subscribe($host, {event: 'click', target}, handle); | ||
test('ESLEventUtils.subscribe subscription correct second time', () => { | ||
expect(listeners2.length).toBe(1); | ||
expect(ESLEventUtils.listeners($host)).toEqual(listeners2); | ||
}); | ||
|
||
test('Resubscribed listener does not observe initial target', () => { | ||
targ1.click(); | ||
expect(handle).not.toBeCalled(); | ||
test('Resubscribed listener does not observe initial target', () => { | ||
$el1.click(); | ||
expect(handle).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('Resubscribed listener observes actual target', () => { | ||
$el2.click(); | ||
expect(handle).toHaveBeenCalled(); | ||
}); | ||
|
||
test('ESLEventUtils does not recreate instance', () => expect(listeners1).toEqual(listeners2)); | ||
}); | ||
|
||
test('Resubscribed listener observes actual target', () => { | ||
targ2.click(); | ||
expect(handle).toBeCalled(); | ||
describe('ESLEventUtils.subscribe resubscribes listener correctly if the source is declared descriptor', () => { | ||
class Test { | ||
onClick() {} | ||
} | ||
|
||
const target = jest.fn(() => $el1); | ||
ESLEventUtils.initDescriptor(Test.prototype, 'onClick', {event: 'click', auto: true, target}); | ||
|
||
const instance = new Test(); | ||
ESLEventUtils.subscribe(instance); | ||
|
||
test('Initial subscription happened', () => { | ||
expect(ESLEventUtils.listeners(instance).length).toBe(1); | ||
}); | ||
|
||
test('Initial subscription have a correct target', () => { | ||
expect(ESLEventUtils.listeners(instance)[0].$targets).toEqual([$el1]); | ||
}); | ||
|
||
// Change target | ||
target.mockImplementation(() => $el2); | ||
// Re-subscription | ||
ESLEventUtils.subscribe(instance); | ||
|
||
test('Re-subscription does not create new listener', () => { | ||
expect(ESLEventUtils.listeners(instance).length).toBe(1); | ||
}); | ||
|
||
test('Re-subscription have a correct target', () => { | ||
expect(ESLEventUtils.listeners(instance)[0].$targets).toEqual([$el2]); | ||
}); | ||
}); | ||
|
||
test('ESLEventUtils does not recreate instance', () => expect(listeners1[0]).toBe(listeners2[0])); | ||
describe('ESLEventUtils.subscribe resubscribes listener correctly if the condition is involved', () => { | ||
class Test { | ||
allowed = false; | ||
onClick() {} | ||
} | ||
ESLEventUtils.initDescriptor(Test.prototype, 'onClick', { | ||
auto: true, | ||
event: 'click', | ||
target: $el1, | ||
condition: ($that: Test) => $that.allowed | ||
}); | ||
|
||
describe('Dynamic condition change to false', () => { | ||
const instance = new Test(); | ||
|
||
test('Initial subscription happened (condition: true)', () => { | ||
instance.allowed = true; | ||
ESLEventUtils.subscribe(instance); | ||
expect(ESLEventUtils.listeners(instance).length).toBe(1); | ||
}); | ||
|
||
test('Subscription removed when condition dynamically ', () => { | ||
instance.allowed = false; | ||
ESLEventUtils.subscribe(instance); | ||
expect(ESLEventUtils.listeners(instance).length).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('Dynamic condition change to true', () => { | ||
const instance = new Test(); | ||
|
||
test('Initial subscription does not happened (condition: false)', () => { | ||
instance.allowed = false; | ||
ESLEventUtils.subscribe(instance); | ||
expect(ESLEventUtils.listeners(instance).length).toBe(0); | ||
}); | ||
|
||
test('Subscription added when condition dynamically ', () => { | ||
instance.allowed = true; | ||
ESLEventUtils.subscribe(instance); | ||
expect(ESLEventUtils.listeners(instance).length).toBe(1); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters