-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add angular-snapshot serializer * Add HTMLElementPlugin until Jest 20 release * Add jest as a peer dep * Update examples with snapshots
- Loading branch information
Showing
13 changed files
with
247 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
function escapeHTML(str) { | ||
return str.replace(/</g, '<').replace(/>/g, '>'); | ||
} | ||
|
||
const HTML_ELEMENT_REGEXP = /(HTML\w*?Element)/; | ||
const test = isHTMLElement; | ||
|
||
function isHTMLElement(value) { | ||
return ( | ||
value !== undefined && | ||
value !== null && | ||
value.nodeType === 1 && | ||
value.constructor !== undefined && | ||
HTML_ELEMENT_REGEXP.test(value.constructor.name) | ||
); | ||
} | ||
|
||
function printChildren(flatChildren, print, indent, colors, opts) { | ||
return flatChildren | ||
.map(node => { | ||
if (typeof node === 'object') { | ||
return print(node, print, indent, colors, opts); | ||
} else if (typeof node === 'string') { | ||
return colors.content.open + escapeHTML(node) + colors.content.close; | ||
} else { | ||
return print(node); | ||
} | ||
}) | ||
.join(opts.edgeSpacing); | ||
} | ||
|
||
function printAttributes(attributes, print, indent, colors, opts) { | ||
return attributes | ||
.sort() | ||
.map(attribute => { | ||
return ( | ||
opts.spacing + | ||
indent(colors.prop.open + attribute.name + colors.prop.close + '=') + | ||
colors.value.open + | ||
`"${attribute.value}"` + | ||
colors.value.close | ||
); | ||
}) | ||
.join(''); | ||
} | ||
|
||
const print = ( | ||
element, | ||
print, | ||
indent, | ||
opts, | ||
colors | ||
) => { | ||
let result = colors.tag.open + '<'; | ||
const elementName = element.tagName.toLowerCase(); | ||
result += elementName + colors.tag.close; | ||
|
||
const hasAttributes = element.attributes && element.attributes.length; | ||
if (hasAttributes) { | ||
const attributes = Array.prototype.slice.call(element.attributes); | ||
result += printAttributes(attributes, print, indent, colors, opts); | ||
} | ||
|
||
const flatChildren = Array.prototype.slice.call(element.children); | ||
if (!flatChildren.length && element.textContent) { | ||
flatChildren.push(element.textContent.trim()); | ||
} | ||
|
||
const closeInNewLine = hasAttributes && !opts.min; | ||
if (flatChildren.length) { | ||
const children = printChildren(flatChildren, print, indent, colors, opts); | ||
result += | ||
colors.tag.open + | ||
(closeInNewLine ? '\n' : '') + | ||
'>' + | ||
colors.tag.close + | ||
(children && opts.edgeSpacing + indent(children) + opts.edgeSpacing) + | ||
colors.tag.open + | ||
'</' + | ||
elementName + | ||
'>' + | ||
colors.tag.close; | ||
} else { | ||
result += | ||
colors.tag.open + (closeInNewLine ? '\n' : ' ') + '/>' + colors.tag.close; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
module.exports = ({print, test}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const printAttributes = (val, attributes, print, indent, colors, opts) => { | ||
return attributes | ||
.sort() | ||
.map(attribute => { | ||
return ( | ||
opts.spacing + | ||
indent(colors.prop.open + attribute + colors.prop.close + '=') + | ||
colors.value.open + | ||
(val.componentInstance[attribute] && | ||
val.componentInstance[attribute].constructor | ||
? '{[Function ' + | ||
val.componentInstance[attribute].constructor.name + | ||
']}' | ||
: `"${val.componentInstance[attribute]}"`) + | ||
colors.value.close | ||
); | ||
}) | ||
.join(''); | ||
}; | ||
|
||
const print = (val, print, indent, opts, colors) => { | ||
let result = ''; | ||
let componentAttrs = ''; | ||
|
||
const componentName = val.componentRef._elDef.element.name; | ||
const componentInstance = print(val.componentInstance); | ||
const nodes = val.componentRef._view.nodes | ||
.filter(node => node.hasOwnProperty('renderElement')) | ||
.map(node => print(node.renderElement)) | ||
.join('\n'); | ||
|
||
const attributes = Object.keys(val.componentInstance); | ||
|
||
if (attributes.length) { | ||
componentAttrs += printAttributes( | ||
val, | ||
attributes, | ||
print, | ||
indent, | ||
colors, | ||
opts | ||
); | ||
} | ||
|
||
return ( | ||
'<' + | ||
componentName + | ||
componentAttrs + | ||
(componentAttrs.length ? '\n' : '') + | ||
'>\n' + | ||
indent(nodes) + | ||
'\n</' + | ||
componentName + | ||
'>' | ||
); | ||
}; | ||
|
||
const test = val => | ||
val !== undefined && | ||
val !== null && | ||
typeof val === 'object' && | ||
Object.prototype.hasOwnProperty.call(val, 'componentRef'); | ||
|
||
module.exports = {print, test}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`AppComponent snaps 1`] = ` | ||
<app-root | ||
hasClass={[Function Boolean]} | ||
title={[Function String]} | ||
> | ||
<div | ||
id="root1" | ||
ng-version="4.0.1" | ||
> | ||
<h1> | ||
<app-calc /> | ||
</h1> | ||
</div> | ||
</app-root> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<h1> | ||
{{title}} | ||
<app-calc | ||
[hasAClass]="hasClass" | ||
></app-calc> | ||
</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
example/src/app/calc/__snapshots__/calc.component.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`CalcComponent should snap 1`] = ` | ||
<app-calc | ||
prop1={[Function Number]} | ||
> | ||
<div | ||
id="root0" | ||
ng-version="4.0.1" | ||
> | ||
<p | ||
class="a-default-class" | ||
ng-reflect-klass="a-default-class" | ||
ng-reflect-ng-class="[object Object]" | ||
> | ||
calc works! | ||
</p> | ||
</div> | ||
</app-calc> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
<p> | ||
<p | ||
class="a-default-class" | ||
[ngClass]="{ | ||
'a-class': hasAClass | ||
}" | ||
> | ||
calc works! | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Component, OnInit, Input } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
@Component({ | ||
selector: 'app-calc', | ||
templateUrl: './calc.component.html', | ||
styleUrls: ['./calc.component.css'] | ||
}) | ||
export class CalcComponent implements OnInit { | ||
@Input() hasAClass; | ||
prop1: number; | ||
observable$: Observable<string>; | ||
|
||
constructor() { } | ||
constructor() { | ||
this.init(); | ||
this.prop1 = 1337; | ||
} | ||
|
||
ngOnInit() { | ||
} | ||
|
||
init() { | ||
return 'Imma method'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters