UNPKG

2.41 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
7 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
8 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
9 * Google as part of the polymer project is also subject to an additional IP
10 * rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12Object.defineProperty(exports, "__esModule", { value: true });
13const chai_1 = require("chai");
14const node_visitor_1 = require("../shady-css/node-visitor");
15class TestNodeVisitor extends node_visitor_1.NodeVisitor {
16 constructor() {
17 super();
18 this.aCallCount = 0;
19 this.bCallCount = 0;
20 }
21 a(a) {
22 this.aCallCount++;
23 if (a.callback) {
24 a.callback();
25 }
26 return 'a';
27 }
28 b(b) {
29 this.bCallCount++;
30 if (b.child) {
31 this.visit(b.child);
32 }
33 return 'b';
34 }
35}
36describe('NodeVisitor', () => {
37 let nodeVisitor;
38 beforeEach(function () {
39 nodeVisitor = new TestNodeVisitor();
40 });
41 it('visits nodes based on their type property', () => {
42 nodeVisitor.visit({ type: 'a' });
43 chai_1.expect(nodeVisitor.aCallCount).to.be.eql(1);
44 chai_1.expect(nodeVisitor.bCallCount).to.be.eql(0);
45 nodeVisitor.visit({ type: 'b' });
46 chai_1.expect(nodeVisitor.aCallCount).to.be.eql(1);
47 chai_1.expect(nodeVisitor.bCallCount).to.be.eql(1);
48 });
49 it('reveals the path of the recursive visitation of nodes', () => {
50 const a1 = {
51 type: 'a',
52 callback: function () {
53 chai_1.expect(nodeVisitor.path).to.be.eql([a1]);
54 }
55 };
56 const a2 = {
57 type: 'a',
58 callback: function () {
59 chai_1.expect(nodeVisitor.path).to.be.eql([b, a2]);
60 }
61 };
62 const b = { type: 'b', child: a2 };
63 nodeVisitor.visit(a1);
64 chai_1.expect(nodeVisitor.aCallCount).to.be.eql(1);
65 nodeVisitor.visit(b);
66 chai_1.expect(nodeVisitor.aCallCount).to.be.eql(2);
67 chai_1.expect(nodeVisitor.bCallCount).to.be.eql(1);
68 });
69});
70//# sourceMappingURL=node-visitor-test.js.map
\No newline at end of file