refactor(core): remove cookie and comment testing utilities from DomAdapters (#32278)

PR Close #32278
This commit is contained in:
Kara Erickson 2019-08-22 16:14:18 -07:00 committed by atscott
parent 4908a5cffc
commit 7bcd42e7be
10 changed files with 29 additions and 52 deletions

View File

@ -19,7 +19,7 @@ import {EmbeddedViewRef} from '@angular/core/src/linker/view_ref';
import {Attribute, Component, ContentChildren, Directive, HostBinding, HostListener, Input, Output, Pipe} from '@angular/core/src/metadata';
import {TestBed, async, fakeAsync, getTestBed, tick} from '@angular/core/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {dispatchEvent, el} from '@angular/platform-browser/testing/src/browser_util';
import {dispatchEvent, el, isCommentNode} from '@angular/platform-browser/testing/src/browser_util';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing';
@ -430,7 +430,7 @@ function declareTests(config?: {useJit: boolean}) {
const childNodesOfWrapper = getDOM().childNodes(fixture.nativeElement);
expect(childNodesOfWrapper.length).toBe(1);
expect(getDOM().isCommentNode(childNodesOfWrapper[0])).toBe(true);
expect(isCommentNode(childNodesOfWrapper[0])).toBe(true);
});
it('should allow to transplant TemplateRefs into other ViewContainers', () => {

View File

@ -10,6 +10,7 @@
import {AfterContentInit, AfterViewInit, Component, ContentChildren, Directive, Input, QueryList, ViewChildren, ɵivyEnabled as ivyEnabled} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {isCommentNode} from '@angular/platform-browser/testing/src/browser_util';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {modifiedInIvy} from '@angular/private/testing';
@ -58,7 +59,7 @@ function declareTests(config?: {useJit: boolean}) {
const el = fixture.nativeElement;
const children = getDOM().childNodes(el);
expect(children.length).toBe(2);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(isCommentNode(children[0])).toBe(true);
expect(getDOM().tagName(children[1]).toUpperCase()).toEqual('P');
});
@ -74,10 +75,10 @@ function declareTests(config?: {useJit: boolean}) {
const el = fixture.nativeElement;
const children = getDOM().childNodes(el);
expect(children.length).toBe(5);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(isCommentNode(children[0])).toBe(true);
expect(children[1]).toHaveText('1');
expect(getDOM().isCommentNode(children[2])).toBe(true);
expect(getDOM().isCommentNode(children[3])).toBe(true);
expect(isCommentNode(children[2])).toBe(true);
expect(isCommentNode(children[3])).toBe(true);
expect(children[4]).toHaveText('2');
});
@ -95,9 +96,9 @@ function declareTests(config?: {useJit: boolean}) {
expect(children.length).toBe(4);
// ngIf anchor
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(isCommentNode(children[0])).toBe(true);
// ng-container anchor
expect(getDOM().isCommentNode(children[1])).toBe(true);
expect(isCommentNode(children[1])).toBe(true);
expect(getDOM().tagName(children[2]).toUpperCase()).toEqual('P');
expect(getDOM().tagName(children[3]).toUpperCase()).toEqual('B');
@ -105,7 +106,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
expect(children.length).toBe(1);
expect(getDOM().isCommentNode(children[0])).toBe(true);
expect(isCommentNode(children[0])).toBe(true);
});
it('should work with static content projection', () => {

View File

@ -19,6 +19,7 @@ import {Request} from '@angular/http/src/static_request';
import {Response} from '@angular/http/src/static_response';
import {URLSearchParams} from '@angular/http/src/url_search_params';
import {ɵgetDOM as getDOM} from '@angular/platform-browser';
import {setCookie} from '@angular/platform-browser/testing/src/browser_util';
let abortSpy: any;
let sendSpy: any;
@ -125,12 +126,12 @@ class MockBrowserXHR extends BrowserXhr {
if (getDOM().supportsCookies()) {
describe('XSRF support', () => {
it('sets an XSRF header by default', () => {
getDOM().setCookie('XSRF-TOKEN', 'magic XSRF value');
setCookie('XSRF-TOKEN', 'magic XSRF value');
backend.createConnection(sampleRequest);
expect(sampleRequest.headers.get('X-XSRF-TOKEN')).toBe('magic XSRF value');
});
it('should allow overwriting of existing headers', () => {
getDOM().setCookie('XSRF-TOKEN', 'magic XSRF value');
setCookie('XSRF-TOKEN', 'magic XSRF value');
sampleRequest.headers.set('X-XSRF-TOKEN', 'already set');
backend.createConnection(sampleRequest);
expect(sampleRequest.headers.get('X-XSRF-TOKEN')).toBe('magic XSRF value');
@ -143,7 +144,7 @@ class MockBrowserXHR extends BrowserXhr {
}]);
it('uses the configured names', () => {
getDOM().setCookie('my cookie', 'XSRF value');
setCookie('my cookie', 'XSRF value');
backend.createConnection(sampleRequest);
expect(sampleRequest.headers.get('X-MY-HEADER')).toBe('XSRF value');
});

View File

@ -300,7 +300,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
return this.isElementNode(el) && el.nodeName === 'TEMPLATE';
}
isTextNode(node: Node): boolean { return node.nodeType === Node.TEXT_NODE; }
isCommentNode(node: Node): boolean { return node.nodeType === Node.COMMENT_NODE; }
isElementNode(node: Node): boolean { return node.nodeType === Node.ELEMENT_NODE; }
hasShadowRoot(node: any): boolean {
return node.shadowRoot != null && node instanceof HTMLElement;
@ -371,12 +370,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
supportsCookies(): boolean { return true; }
getCookie(name: string): string|null { return parseCookieValue(document.cookie, name); }
setCookie(name: string, value: string) {
// document.cookie is magical, assigning into it assigns/overrides one cookie value, but does
// not clear other cookies.
document.cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
}
}
let baseElement: HTMLElement|null = null;

View File

@ -128,7 +128,6 @@ export abstract class DomAdapter {
abstract elementMatches(n: any, selector: string): boolean;
abstract isTemplateElement(el: any): boolean;
abstract isTextNode(node: any): boolean;
abstract isCommentNode(node: any): boolean;
abstract isElementNode(node: any): boolean;
abstract hasShadowRoot(node: any): boolean;
abstract isShadowRoot(node: any): boolean;
@ -153,7 +152,7 @@ export abstract class DomAdapter {
abstract getTransitionEnd(): string;
abstract supportsAnimation(): boolean;
// Used by CookieXSRFStrategy
abstract supportsCookies(): boolean;
abstract getCookie(name: string): string|null;
abstract setCookie(name: string, value: string): any;
}

View File

@ -1,22 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {describe, expect, it} from '@angular/core/testing/src/testing_internal';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
{
describe('cookies', () => {
if (isNode) return;
it('sets cookie values', () => {
getDOM().setCookie('my test cookie', 'my test value');
getDOM().setCookie('my other cookie', 'my test value 2');
expect(getDOM().getCookie('my test cookie')).toBe('my test value');
});
});
}

View File

@ -157,8 +157,8 @@ export function stringifyElement(el: any /** TODO #9100 */): string {
if (_selfClosingTags.indexOf(tagName) == -1) {
result += `</${tagName}>`;
}
} else if (getDOM().isCommentNode(el)) {
result += `<!--${getDOM().nodeValue(el)}-->`;
} else if (isCommentNode(el)) {
result += `<!--${el.nodeValue}-->`;
} else {
result += getDOM().getText(el);
}
@ -169,3 +169,13 @@ export function stringifyElement(el: any /** TODO #9100 */): string {
export function createNgZone(): NgZone {
return new NgZone({enableLongStackTrace: true});
}
export function isCommentNode(node: Node): boolean {
return node.nodeType === Node.COMMENT_NODE;
}
export function setCookie(name: string, value: string) {
// document.cookie is magical, assigning into it assigns/overrides one cookie value, but does
// not clear other cookies.
document.cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
}

View File

@ -10,6 +10,7 @@
import {Type, ɵglobal as global} from '@angular/core';
import {ComponentFixture} from '@angular/core/testing';
import {By, ɵgetDOM as getDOM} from '@angular/platform-browser';
import {isCommentNode} from '@angular/platform-browser/testing/src/browser_util';
@ -285,7 +286,7 @@ function elementText(n: any): string {
return n.map(elementText).join('');
}
if (getDOM().isCommentNode(n)) {
if (isCommentNode(n)) {
return '';
}

View File

@ -88,9 +88,6 @@ export class DominoAdapter extends BrowserDomAdapter {
getShadowRoot(el: any): DocumentFragment { return el.shadowRoot; }
isTextNode(node: any): boolean { return node.nodeType === DominoAdapter.defaultDoc.TEXT_NODE; }
isCommentNode(node: any): boolean {
return node.nodeType === DominoAdapter.defaultDoc.COMMENT_NODE;
}
isElementNode(node: any): boolean {
return node ? node.nodeType === DominoAdapter.defaultDoc.ELEMENT_NODE : false;
}
@ -220,5 +217,4 @@ export class DominoAdapter extends BrowserDomAdapter {
supportsCookies(): boolean { return false; }
getCookie(name: string): string { throw _notImplemented('getCookie'); }
setCookie(name: string, value: string) { throw _notImplemented('setCookie'); }
}

View File

@ -132,7 +132,6 @@ export class WorkerDomAdapter extends DomAdapter {
elementMatches(n: any, selector: string): boolean { throw 'not implemented'; }
isTemplateElement(el: any): boolean { throw 'not implemented'; }
isTextNode(node: any): boolean { throw 'not implemented'; }
isCommentNode(node: any): boolean { throw 'not implemented'; }
isElementNode(node: any): boolean { throw 'not implemented'; }
hasShadowRoot(node: any): boolean { throw 'not implemented'; }
isShadowRoot(node: any): boolean { throw 'not implemented'; }
@ -159,5 +158,4 @@ export class WorkerDomAdapter extends DomAdapter {
supportsCookies(): boolean { return false; }
getCookie(name: string): string { throw 'not implemented'; }
setCookie(name: string, value: string) { throw 'not implemented'; }
}