|
| 1 | +import { |
| 2 | + beforeEach, |
| 3 | + it, |
| 4 | + describe, |
| 5 | + expect, |
| 6 | + inject |
| 7 | +} from '@angular/core/testing'; |
| 8 | +import { ASTNode } from '../ast'; |
| 9 | +import { CssSelector } from './css-selector'; |
| 10 | +import { CssNodeMatcher } from './css-node-matcher'; |
| 11 | + |
| 12 | +describe('CssNodeMatcher', () => { |
| 13 | + |
| 14 | + var node: ASTNode; |
| 15 | + beforeEach(() => { |
| 16 | + node = { |
| 17 | + attrs: [ |
| 18 | + { name: 'foo', value: 'bar' }, |
| 19 | + { name: 'class', value: 'dialog modal--drop' }, |
| 20 | + { name: 'id', value: 'dialog-id' } |
| 21 | + ], |
| 22 | + nodeName: 'div', |
| 23 | + parentNode: null |
| 24 | + }; |
| 25 | + }); |
| 26 | + |
| 27 | + describe('successful match', () => { |
| 28 | + |
| 29 | + it('should match any node with empty selector', () => { |
| 30 | + const emptySelector = CssSelector.parse(''); |
| 31 | + const selector = new CssNodeMatcher(emptySelector); |
| 32 | + expect(selector.match(node)).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should match basic element selector', () => { |
| 36 | + const elementSelector = CssSelector.parse('div'); |
| 37 | + const selector = new CssNodeMatcher(elementSelector); |
| 38 | + expect(selector.match(node)).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should match attribute selector', () => { |
| 42 | + const attrSelector = CssSelector.parse('[foo=bar]'); |
| 43 | + const selector = new CssNodeMatcher(attrSelector); |
| 44 | + expect(selector.match(node)).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should match attribute selector when no value is provided', () => { |
| 48 | + const attrSelector = CssSelector.parse('[foo]'); |
| 49 | + const selector = new CssNodeMatcher(attrSelector); |
| 50 | + expect(selector.match(node)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should match class selector', () => { |
| 54 | + const classSelector = CssSelector.parse('.dialog'); |
| 55 | + const selector = new CssNodeMatcher(classSelector); |
| 56 | + expect(selector.match(node)).toBe(true); |
| 57 | + const complexClassSelector = CssSelector.parse('.dialog.modal--drop'); |
| 58 | + const complexSelector = new CssNodeMatcher(complexClassSelector); |
| 59 | + expect(complexSelector.match(node)).toBe(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should match element by id', () => { |
| 63 | + const idSelector = CssSelector.parse('#dialog-id'); |
| 64 | + const selector = new CssNodeMatcher(idSelector); |
| 65 | + expect(selector.match(node)).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + }); |
| 69 | + |
| 70 | + describe('unsuccessful match', () => { |
| 71 | + |
| 72 | + it('should fail when different element is used', () => { |
| 73 | + const elementSelector = CssSelector.parse('span'); |
| 74 | + const selector = new CssNodeMatcher(elementSelector); |
| 75 | + expect(selector.match(node)).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should fail when different attribute selector is provided', () => { |
| 79 | + const attrSelector = CssSelector.parse('[foo=qux]'); |
| 80 | + const selector = new CssNodeMatcher(attrSelector); |
| 81 | + expect(selector.match(node)).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should fail when non-matching class selector is used', () => { |
| 85 | + const classSelector = CssSelector.parse('.modal'); |
| 86 | + const selector = new CssNodeMatcher(classSelector); |
| 87 | + expect(selector.match(node)).toBe(false); |
| 88 | + const complexClassSelector = CssSelector.parse('.dialog.modal-drop'); |
| 89 | + const complexSelector = new CssNodeMatcher(complexClassSelector); |
| 90 | + expect(complexSelector.match(node)).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should fail when superset of attributes is used in selector', () => { |
| 94 | + const cssSelector = CssSelector.parse('[foo=bar][baz=bar]'); |
| 95 | + const selector = new CssNodeMatcher(cssSelector); |
| 96 | + expect(selector.match(node)).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should fail when superset of attributes is used in selector', () => { |
| 100 | + const cssSelector = CssSelector.parse('[no-render]'); |
| 101 | + const selector = new CssNodeMatcher(cssSelector); |
| 102 | + expect(selector.match(node)).toBe(false); |
| 103 | + }) |
| 104 | + |
| 105 | + it('should fail match by id when element has no id', () => { |
| 106 | + const cssSelector = CssSelector.parse('#foo'); |
| 107 | + const selector = new CssNodeMatcher(cssSelector); |
| 108 | + const node1: ASTNode = { |
| 109 | + nodeName: 'div', |
| 110 | + parentNode: null, |
| 111 | + attrs: [] |
| 112 | + }; |
| 113 | + const node2: ASTNode = { |
| 114 | + attrs: [ |
| 115 | + { name: 'not-id', value: '' } |
| 116 | + ], |
| 117 | + nodeName: 'div', |
| 118 | + parentNode: null |
| 119 | + }; |
| 120 | + expect(selector.match(node1)).toBe(false); |
| 121 | + expect(selector.match(node2)).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
0 commit comments