-
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.
feat: component serializer without ng-* attributes (#97)
* feat: component serializer without Angular specific attributes like _nghost and ng-reflect-* * fix(ci): disable cache in example app to ensure preset currency
- Loading branch information
1 parent
61c391a
commit 1079528
Showing
33 changed files
with
316 additions
and
35 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
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,4 @@ | ||
node_modules | ||
InlineHtmlStripStylesTransformer.js | ||
*.log | ||
.idea |
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,30 @@ | ||
'use strict'; | ||
|
||
const jestDOMElementSerializer = require('pretty-format').plugins.DOMElement; | ||
|
||
const attributesToRemovePatterns = ['ng-reflect', '_nghost', '_ngcontent', 'ng-version']; | ||
|
||
const hasAttributesToRemove = (attribute) => | ||
attributesToRemovePatterns | ||
.some(removePattern => attribute.name.startsWith(removePattern)); | ||
|
||
const serialize = (node, ...rest) => { | ||
const nodeCopy = node.cloneNode(true); | ||
Object.values(nodeCopy.attributes) | ||
.filter(hasAttributesToRemove) | ||
.forEach(attribute => nodeCopy.attributes.removeNamedItem(attribute.name)); | ||
|
||
return jestDOMElementSerializer.serialize(nodeCopy, ...rest); | ||
}; | ||
|
||
const serializeTestFn = (val) => { | ||
return val.attributes !== undefined && Object.values(val.attributes) | ||
.some(hasAttributesToRemove) | ||
}; | ||
const test = val => | ||
jestDOMElementSerializer.test(val) && serializeTestFn(val); | ||
|
||
module.exports = { | ||
serialize: serialize, | ||
test: 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
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
Empty file.
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
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
Empty file.
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 was deleted.
Oops, something went wrong.
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
36 changes: 36 additions & 0 deletions
36
example/src/app/medium/__snapshots__/medium.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,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MediumComponent snapshot test 1`] = ` | ||
<app-medium | ||
anotherVar="false" | ||
someVar={[Function Boolean]} | ||
> | ||
<div | ||
class="some-wrapper-class" | ||
> | ||
diary works! | ||
<div | ||
class="inner-class" | ||
> | ||
<div> | ||
another case | ||
</div> | ||
<span> | ||
some html | ||
</span> | ||
</div> | ||
<div> | ||
one more case case | ||
</div> | ||
<div> | ||
<app-child-component | ||
someinput="someVar" | ||
> | ||
stubbedBody | ||
</app-child-component> | ||
</div> | ||
</div> | ||
</app-medium> | ||
`; |
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 @@ | ||
import { Component, Input } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-child-component', | ||
template: ` | ||
<div class="child"> | ||
{{ someInput }} | ||
<div> | ||
rly | ||
<p>complex</p> | ||
<div>component | ||
<div>oh my god!</div> | ||
</div> | ||
</div> | ||
</div> | ||
` | ||
}) | ||
export class ChildComponent { | ||
@Input() someInput = null; | ||
} |
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,34 @@ | ||
/* tslint:disable:no-unused-variable */ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { MediumComponent } from './medium.component'; | ||
import { ChildComponent } from './child.component'; | ||
|
||
describe('MediumComponent', () => { | ||
let component: MediumComponent; | ||
let fixture: ComponentFixture<MediumComponent>; | ||
|
||
beforeEach( | ||
async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [MediumComponent, ChildComponent], | ||
}) | ||
.overrideComponent(ChildComponent, {set: {template: 'stubbedBody'}}) | ||
.compileComponents(); | ||
}), | ||
); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(MediumComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('snapshot test', () => { | ||
expect(fixture).toMatchSnapshot(); | ||
}); | ||
}); |
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,21 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-medium', | ||
template: ` | ||
<div class="some-wrapper-class"> | ||
diary works! | ||
<div *ngIf="someVar" class="inner-class"> | ||
<div *ngIf="anotherVar" class="inner-hidden"></div> | ||
<div>another case</div> | ||
<span>some html</span> | ||
</div> | ||
<div>one more case case</div> | ||
<div><app-child-component someInput="someVar"></app-child-component></div> | ||
</div> | ||
` | ||
}) | ||
export class MediumComponent { | ||
someVar = true; | ||
anotherVar = false; | ||
} |
12 changes: 12 additions & 0 deletions
12
example/src/app/ng-reflect-as-text/__snapshots__/ng-reflect-as-text.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,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`NgReflectAsTextComponent snapshots 1`] = ` | ||
<app-ng-reflect-as-text> | ||
<p | ||
data-some-attr="ng-reflect-as-attr-arg works" | ||
> | ||
ng-reflect-as-text works! | ||
</p> | ||
</app-ng-reflect-as-text> | ||
`; |
3 changes: 3 additions & 0 deletions
3
example/src/app/ng-reflect-as-text/ng-reflect-as-text.component.html
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,3 @@ | ||
<p data-some-attr="ng-reflect-as-attr-arg works"> | ||
ng-reflect-as-text works! | ||
</p> |
25 changes: 25 additions & 0 deletions
25
example/src/app/ng-reflect-as-text/ng-reflect-as-text.component.spec.ts
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,25 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { NgReflectAsTextComponent } from './ng-reflect-as-text.component'; | ||
|
||
describe('NgReflectAsTextComponent', () => { | ||
let component: NgReflectAsTextComponent; | ||
let fixture: ComponentFixture<NgReflectAsTextComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ NgReflectAsTextComponent ] | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(NgReflectAsTextComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('snapshots', () => { | ||
expect(fixture).toMatchSnapshot(); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
example/src/app/ng-reflect-as-text/ng-reflect-as-text.component.ts
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,7 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-ng-reflect-as-text', | ||
templateUrl: './ng-reflect-as-text.component.html' | ||
}) | ||
export class NgReflectAsTextComponent {} |
32 changes: 32 additions & 0 deletions
32
example/src/app/simple-with-styles/__snapshots__/simple-with-styles.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,32 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SimpleWithStylesComponent should generate snapshot without nghost/ngcontent 1`] = ` | ||
<app-simple-with-styles> | ||
<p> | ||
simple-with-styles works! | ||
</p><div | ||
class="some-class" | ||
> | ||
<app-child-component> | ||
<div | ||
class="child" | ||
> | ||
<div> | ||
rly | ||
<p> | ||
complex | ||
</p> | ||
<div> | ||
component | ||
<div> | ||
oh my god! | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</app-child-component> | ||
</div> | ||
</app-simple-with-styles> | ||
`; |
6 changes: 6 additions & 0 deletions
6
example/src/app/simple-with-styles/simple-with-styles.component.html
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,6 @@ | ||
<p> | ||
simple-with-styles works! | ||
</p> | ||
<div class="some-class"> | ||
<app-child-component></app-child-component> | ||
</div> |
Oops, something went wrong.