|
| 1 | +import {DOM} from 'angular2/src/core/dom/dom_adapter'; |
| 2 | +import {global} from 'angular2/src/core/facade/lang'; |
| 3 | + |
| 4 | + |
| 5 | +export interface NgMatchers extends jasmine.Matchers { |
| 6 | + toBePromise(): boolean; |
| 7 | + toBeAnInstanceOf(expected: any): boolean; |
| 8 | + toHaveText(expected: any): boolean; |
| 9 | + toHaveCssClass(expected: any): boolean; |
| 10 | + toImplement(expected: any): boolean; |
| 11 | + toContainError(expected: any): boolean; |
| 12 | + toThrowErrorWith(expectedMessage: any): boolean; |
| 13 | + not: NgMatchers; |
| 14 | +} |
| 15 | + |
| 16 | +var _global: jasmine.GlobalPolluter = <any>(typeof window === 'undefined' ? global : window); |
| 17 | + |
| 18 | +export var expect: (actual: any) => NgMatchers = <any>_global.expect; |
| 19 | + |
| 20 | + |
| 21 | +// Some Map polyfills don't polyfill Map.toString correctly, which |
| 22 | +// gives us bad error messages in tests. |
| 23 | +// The only way to do this in Jasmine is to monkey patch a method |
| 24 | +// to the object :-( |
| 25 | +Map.prototype['jasmineToString'] = function() { |
| 26 | + var m = this; |
| 27 | + if (!m) { |
| 28 | + return '' + m; |
| 29 | + } |
| 30 | + var res = []; |
| 31 | + m.forEach((v, k) => { res.push(`${k}:${v}`); }); |
| 32 | + return `{ ${res.join(',')} }`; |
| 33 | +}; |
| 34 | + |
| 35 | +_global.beforeEach(function() { |
| 36 | + jasmine.addMatchers({ |
| 37 | + // Custom handler for Map as Jasmine does not support it yet |
| 38 | + toEqual: function(util, customEqualityTesters) { |
| 39 | + return { |
| 40 | + compare: function(actual, expected) { |
| 41 | + return {pass: util.equals(actual, expected, [compareMap])}; |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + function compareMap(actual, expected) { |
| 46 | + if (actual instanceof Map) { |
| 47 | + var pass = actual.size === expected.size; |
| 48 | + if (pass) { |
| 49 | + actual.forEach((v, k) => { pass = pass && util.equals(v, expected.get(k)); }); |
| 50 | + } |
| 51 | + return pass; |
| 52 | + } else { |
| 53 | + return undefined; |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
| 57 | + |
| 58 | + toBePromise: function() { |
| 59 | + return { |
| 60 | + compare: function(actual, expectedClass) { |
| 61 | + var pass = typeof actual === 'object' && typeof actual.then === 'function'; |
| 62 | + return {pass: pass, get message() { return 'Expected ' + actual + ' to be a promise'; }}; |
| 63 | + } |
| 64 | + }; |
| 65 | + }, |
| 66 | + |
| 67 | + toBeAnInstanceOf: function() { |
| 68 | + return { |
| 69 | + compare: function(actual, expectedClass) { |
| 70 | + var pass = typeof actual === 'object' && actual instanceof expectedClass; |
| 71 | + return { |
| 72 | + pass: pass, |
| 73 | + get message() { |
| 74 | + return 'Expected ' + actual + ' to be an instance of ' + expectedClass; |
| 75 | + } |
| 76 | + }; |
| 77 | + } |
| 78 | + }; |
| 79 | + }, |
| 80 | + |
| 81 | + toHaveText: function() { |
| 82 | + return { |
| 83 | + compare: function(actual, expectedText) { |
| 84 | + var actualText = elementText(actual); |
| 85 | + return { |
| 86 | + pass: actualText == expectedText, |
| 87 | + get message() { return 'Expected ' + actualText + ' to be equal to ' + expectedText; } |
| 88 | + }; |
| 89 | + } |
| 90 | + }; |
| 91 | + }, |
| 92 | + |
| 93 | + toHaveCssClass: function() { |
| 94 | + return {compare: buildError(false), negativeCompare: buildError(true)}; |
| 95 | + |
| 96 | + function buildError(isNot) { |
| 97 | + return function(actual, className) { |
| 98 | + return { |
| 99 | + pass: DOM.hasClass(actual, className) == !isNot, |
| 100 | + get message() { |
| 101 | + return `Expected ${actual.outerHTML} ${isNot ? 'not ' : ''}to contain the CSS class "${className}"`; |
| 102 | + } |
| 103 | + }; |
| 104 | + }; |
| 105 | + } |
| 106 | + }, |
| 107 | + |
| 108 | + toContainError: function() { |
| 109 | + return { |
| 110 | + compare: function(actual, expectedText) { |
| 111 | + var errorMessage = actual.toString(); |
| 112 | + return { |
| 113 | + pass: errorMessage.indexOf(expectedText) > -1, |
| 114 | + get message() { return 'Expected ' + errorMessage + ' to contain ' + expectedText; } |
| 115 | + }; |
| 116 | + } |
| 117 | + }; |
| 118 | + }, |
| 119 | + |
| 120 | + toThrowErrorWith: function() { |
| 121 | + return { |
| 122 | + compare: function(actual, expectedText) { |
| 123 | + try { |
| 124 | + actual(); |
| 125 | + return { |
| 126 | + pass: false, |
| 127 | + get message() { return "Was expected to throw, but did not throw"; } |
| 128 | + }; |
| 129 | + } catch (e) { |
| 130 | + var errorMessage = e.toString(); |
| 131 | + return { |
| 132 | + pass: errorMessage.indexOf(expectedText) > -1, |
| 133 | + get message() { return 'Expected ' + errorMessage + ' to contain ' + expectedText; } |
| 134 | + }; |
| 135 | + } |
| 136 | + } |
| 137 | + }; |
| 138 | + }, |
| 139 | + |
| 140 | + toImplement: function() { |
| 141 | + return { |
| 142 | + compare: function(actualObject, expectedInterface) { |
| 143 | + var objProps = Object.keys(actualObject.constructor.prototype); |
| 144 | + var intProps = Object.keys(expectedInterface.prototype); |
| 145 | + |
| 146 | + var missedMethods = []; |
| 147 | + intProps.forEach((k) => { |
| 148 | + if (!actualObject.constructor.prototype[k]) missedMethods.push(k); |
| 149 | + }); |
| 150 | + |
| 151 | + return { |
| 152 | + pass: missedMethods.length == 0, |
| 153 | + get message() { |
| 154 | + return 'Expected ' + actualObject + ' to have the following methods: ' + |
| 155 | + missedMethods.join(", "); |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
| 159 | + }; |
| 160 | + } |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +function elementText(n) { |
| 165 | + var hasNodes = (n) => { |
| 166 | + var children = DOM.childNodes(n); |
| 167 | + return children && children.length > 0; |
| 168 | + }; |
| 169 | + |
| 170 | + if (n instanceof Array) { |
| 171 | + return n.map(elementText).join(""); |
| 172 | + } |
| 173 | + |
| 174 | + if (DOM.isCommentNode(n)) { |
| 175 | + return ''; |
| 176 | + } |
| 177 | + |
| 178 | + if (DOM.isElementNode(n) && DOM.tagName(n) == 'CONTENT') { |
| 179 | + return elementText(Array.prototype.slice.apply(DOM.getDistributedNodes(n))); |
| 180 | + } |
| 181 | + |
| 182 | + if (DOM.hasShadowRoot(n)) { |
| 183 | + return elementText(DOM.childNodesAsList(DOM.getShadowRoot(n))); |
| 184 | + } |
| 185 | + |
| 186 | + if (hasNodes(n)) { |
| 187 | + return elementText(DOM.childNodesAsList(n)); |
| 188 | + } |
| 189 | + |
| 190 | + return DOM.getText(n); |
| 191 | +} |
0 commit comments