-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathsimple-context.spec.js
More file actions
87 lines (77 loc) · 3.49 KB
/
simple-context.spec.js
File metadata and controls
87 lines (77 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { createElement } from 'lwc';
import { installCustomContext, setCustomContext } from 'x/simpleProvider';
import Consumer from 'x/simpleConsumer';
import { jasmine } from '../../helpers/jasmine.js';
describe('Simple Custom Context Provider', () => {
it('should be install-able on any dom element', function () {
const div = document.createElement('div');
const elm = createElement('x-consumer', { is: Consumer });
document.body.appendChild(div);
installCustomContext(div);
setCustomContext(div, 'ready');
div.appendChild(elm);
expect(elm.shadowRoot.textContent).toBe('ready');
});
it('should call disconnect when no context value is provided', function () {
const div = document.createElement('div');
const elm = createElement('x-consumer', { is: Consumer });
document.body.appendChild(div);
const spy = {
connected: jasmine.createSpy('connected'),
disconnected: jasmine.createSpy('disconnected'),
};
installCustomContext(div, spy, true);
// not providing context intentionally
div.appendChild(elm);
expect(spy.connected).toHaveBeenCalledTimes(1);
div.removeChild(elm);
expect(spy.disconnected).toHaveBeenCalledTimes(1);
});
it('should not call disconnect with same consumer when multiple contexts are set', function () {
const div = document.createElement('div');
const elm = createElement('x-consumer', { is: Consumer });
document.body.appendChild(div);
const spy = {
disconnected: jasmine.createSpy('disconnected'),
};
installCustomContext(div, spy);
setCustomContext(div, 'almost ready');
div.appendChild(elm);
setCustomContext(div, 'ready');
expect(() => {
div.removeChild(elm);
}).not.toThrowError();
expect(spy.disconnected).toHaveBeenCalledTimes(1);
});
it('should provide "missing" as the default value when no provider is installed', function () {
const div = document.createElement('div');
const elm = createElement('x-consumer', { is: Consumer });
document.body.appendChild(div);
div.appendChild(elm);
expect(elm.shadowRoot.textContent).toBe('missing');
});
it('should provide "pending" when provide is installed by no value was provided', function () {
const div = document.createElement('div');
const elm = createElement('x-consumer', { is: Consumer });
document.body.appendChild(div);
installCustomContext(div);
div.appendChild(elm);
expect(elm.shadowRoot.textContent).toBe('pending');
});
it('should use closest context when installed in a hierarchy of targets', function () {
const div = document.createElement('div');
div.innerHTML = '<div class="parent-ctx"><div class="child-ctx"></div></div>';
const elm = createElement('x-consumer', { is: Consumer });
const elm2 = createElement('x-consumer', { is: Consumer });
const childTarget = div.querySelector('.child-ctx');
document.body.appendChild(div);
installCustomContext(div);
installCustomContext(childTarget);
setCustomContext(div, 'parent');
setCustomContext(childTarget, 'child');
div.appendChild(elm);
childTarget.appendChild(elm2);
expect(elm.shadowRoot.textContent).toBe('parent');
expect(elm2.shadowRoot.textContent).toBe('child');
});
});