/
js.js
55 lines (44 loc) · 2 KB
/
js.js
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
let should = chai.should();
describe('App', function() {
describe('bind()', function () {
it('instantiates controllers from the markup', function() {
// GIVEN a markup with a JS controller
const containerElement = document.getElementById('container');
containerElement.innerHTML = `
<div id="test" data-js-test='{"param": "value"}'>
</div>
`;
// WHEN you instantiate and bind all JS controllers in the container
osm_app.bind(containerElement);
// THEN instantiated controller options are assigned as defined
// in the markup. There are different ways to access the controller
// object
const testElement = document.getElementById('test');
let test = osm_app.controller(testElement, 'test');
should.exist(test);
test = osm_app.controller('#test', 'test');
should.exist(test);
test = osm_app.controller('#test:test');
should.exist(test);
test.options.param.should.equal('value');
});
it('attaches controller event handlers', function() {
// GIVEN a markup with a JS controller
const containerElement = document.getElementById('container');
containerElement.innerHTML = `
<div id="test" data-js-test='{"param": "value"}'>
</div>
`;
// WHEN you instantiate and bind all JS controllers in the container
osm_app.bind(containerElement);
// AND trigger an event
const testElement = document.getElementById('test');
testElement.dispatchEvent(new MouseEvent('click'));
// THEN instantiated controller event handler is triggered
osm_app.controller('#test:test').clicked.should.equal(true);
});
afterEach(function() {
document.getElementById('container').innerHTML = '';
});
});
});