|
| 1 | +'use strict'; // necessary for es6 output in node |
| 2 | + |
| 3 | +import { browser, element, by, ExpectedConditions as EC } from 'protractor'; |
| 4 | + |
| 5 | +describe('Change Detection guide', () => { |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + |
| 9 | + // setInterval() used in the code makes Protractor mistakenly think we're not |
| 10 | + // finished loading unless we turn this on. |
| 11 | + browser.ignoreSynchronization = true; |
| 12 | + |
| 13 | + browser.get('/'); |
| 14 | + return browser.wait(EC.presenceOf(element(by.css('[ng-version]')))); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('Basic Example', () => { |
| 18 | + |
| 19 | + it('displays a counter that can be incremented and decremented', () => { |
| 20 | + const component = element(by.tagName('hero-counter')); |
| 21 | + const counter = component.element(by.css('span')); |
| 22 | + |
| 23 | + expect(counter.getText()).toEqual('5'); |
| 24 | + |
| 25 | + component.element(by.buttonText('+')).click(); |
| 26 | + expect(counter.getText()).toEqual('6'); |
| 27 | + component.element(by.buttonText('-')).click(); |
| 28 | + expect(counter.getText()).toEqual('5'); |
| 29 | + }); |
| 30 | + |
| 31 | + }); |
| 32 | + |
| 33 | + describe('Broken name badge example', () => { |
| 34 | + |
| 35 | + it('causes an error', () => { |
| 36 | + const errorDump = element(by.id('bootstrapError')); |
| 37 | + expect(errorDump.getText()).toContain('HeroNameBadgeBrokenComponent'); |
| 38 | + expect(errorDump.getText()).toContain('Expression has changed after it was checked'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('still displays the bound data', () => { |
| 42 | + const component = element(by.tagName('hero-name-badge-broken')); |
| 43 | + expect(component.element(by.css('h4')).getText()).toEqual('Anonymous details'); |
| 44 | + }); |
| 45 | + |
| 46 | + }); |
| 47 | + |
| 48 | + describe('Fixed name badge example', () => { |
| 49 | + |
| 50 | + it('displays the bound data', () => { |
| 51 | + const component = element(by.tagName('hero-name-badge')); |
| 52 | + expect(component.element(by.css('h4')).getText()).toEqual('details'); |
| 53 | + expect(component.element(by.css('p')).getText()).toEqual('Name: Anonymous'); |
| 54 | + }); |
| 55 | + |
| 56 | + }); |
| 57 | + |
| 58 | + describe('OnPush', () => { |
| 59 | + |
| 60 | + describe('with immutable string inputs', () => { |
| 61 | + |
| 62 | + it('displays the bound data', () => { |
| 63 | + const component = element(by.tagName('hero-search-result')); |
| 64 | + const match = component.element(by.css('.match')); |
| 65 | + expect(match.getText()).toEqual('indsto'); |
| 66 | + }); |
| 67 | + |
| 68 | + }); |
| 69 | + |
| 70 | + describe('with input mutations', () => { |
| 71 | + |
| 72 | + it('does not display the mutations', () => { |
| 73 | + const component = element(by.tagName('hero-manager-mutable')); |
| 74 | + |
| 75 | + expect(component.element(by.cssContainingText('li', 'Windstorm')).isPresent()).toBe(true); |
| 76 | + expect(component.element(by.cssContainingText('li', 'Magneta')).isPresent()).toBe(true); |
| 77 | + component.element(by.buttonText('Add one more')).click(); |
| 78 | + expect(component.element(by.cssContainingText('li', 'Bombasto')).isPresent()).toBe(false); |
| 79 | + |
| 80 | + }); |
| 81 | + |
| 82 | + }); |
| 83 | + |
| 84 | + describe('with immutable array input', () => { |
| 85 | + |
| 86 | + it('displays the changes', () => { |
| 87 | + const component = element(by.tagName('hero-manager-immutable')); |
| 88 | + |
| 89 | + expect(component.element(by.cssContainingText('li', 'Windstorm')).isPresent()).toBe(true); |
| 90 | + expect(component.element(by.cssContainingText('li', 'Magneta')).isPresent()).toBe(true); |
| 91 | + component.element(by.buttonText('Add one more')).click(); |
| 92 | + expect(component.element(by.cssContainingText('li', 'Bombasto')).isPresent()).toBe(true); |
| 93 | + |
| 94 | + }); |
| 95 | + |
| 96 | + }); |
| 97 | + |
| 98 | + describe('with events', () => { |
| 99 | + |
| 100 | + it('displays the changes', () => { |
| 101 | + const component = element(by.tagName('hero-counter-onpush')); |
| 102 | + const counter = component.element(by.css('span')); |
| 103 | + |
| 104 | + expect(counter.getText()).toEqual('5'); |
| 105 | + |
| 106 | + component.element(by.buttonText('+')).click(); |
| 107 | + expect(counter.getText()).toEqual('6'); |
| 108 | + component.element(by.buttonText('-')).click(); |
| 109 | + expect(counter.getText()).toEqual('5'); |
| 110 | + }); |
| 111 | + |
| 112 | + }); |
| 113 | + |
| 114 | + describe('with explicit markForDetection()', () => { |
| 115 | + |
| 116 | + it('does not detect setInterval() when not used', () => { |
| 117 | + const component = element(by.tagName('hero-counter-auto-broken')); |
| 118 | + browser.sleep(300); // There's an interval of 100ms inside the component. |
| 119 | + expect(component.getText()).toEqual('Number of heroes: 5'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('does detect setInterval() when used', () => { |
| 123 | + const component = element(by.tagName('hero-counter-auto')); |
| 124 | + browser.sleep(300); // There's an interval of 100ms inside the component. |
| 125 | + expect(component.getText()).not.toEqual('Number of heroes: 5'); |
| 126 | + expect(component.getText()).toMatch(/Number of heroes: \d+/); |
| 127 | + }); |
| 128 | + |
| 129 | + it('detects on evented library callbacks', () => { |
| 130 | + const component = element(by.tagName('hero-name-badge-evented')); |
| 131 | + expect(component.element(by.cssContainingText('h4', 'Windstorm details')).isPresent()).toBe(true); |
| 132 | + element(by.buttonText('Rename')).click(); |
| 133 | + expect(component.element(by.cssContainingText('h4', 'Magneta details')).isPresent()).toBe(true); |
| 134 | + }); |
| 135 | + |
| 136 | + }); |
| 137 | + |
| 138 | + describe('detached', () => { |
| 139 | + |
| 140 | + it('does not pick up changes automatically', () => { |
| 141 | + const component = element(by.tagName('hero-name-badge-detached')); |
| 142 | + expect(component.element(by.css('h4')).getText()).toEqual('Windstorm details'); |
| 143 | + element(by.buttonText('Rename detached')).click(); |
| 144 | + expect(component.element(by.css('h4')).getText()).toEqual('Windstorm details'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('starts picking up changes again when reattached', () => { |
| 148 | + const component = element(by.tagName('hero-counter-live')); |
| 149 | + const count = component.element(by.css('.count')); |
| 150 | + |
| 151 | + const text1 = count.getText(); |
| 152 | + browser.sleep(100); |
| 153 | + component.element(by.buttonText('Toggle live update')).click(); |
| 154 | + const text2 = count.getText(); |
| 155 | + browser.sleep(100); |
| 156 | + const text3 = count.getText(); |
| 157 | + |
| 158 | + expect(text1).not.toEqual(text2); |
| 159 | + expect(text2).toEqual(text3); |
| 160 | + }); |
| 161 | + |
| 162 | + it('can be used for throttling by explicitly detecting with an interval', () => { |
| 163 | + const component = element(by.tagName('hero-counter-throttled')); |
| 164 | + const count = component.element(by.css('.count')); |
| 165 | + |
| 166 | + const text1 = count.getText(); |
| 167 | + browser.sleep(100); |
| 168 | + const text2 = count.getText(); |
| 169 | + browser.sleep(100); |
| 170 | + const text3 = count.getText(); |
| 171 | + |
| 172 | + Promise.all([text1, text2, text3]).then(([t1, t2, t3]) => { |
| 173 | + let differences = 0; |
| 174 | + if (t1 !== t2) { |
| 175 | + differences++; |
| 176 | + } |
| 177 | + if (t2 !== t3) { |
| 178 | + differences++; |
| 179 | + } |
| 180 | + expect(differences).toBeLessThan(2); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + }); |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + }); |
| 190 | + |
| 191 | +}); |
0 commit comments