-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
379 additions
and
96 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,74 @@ | ||
--- | ||
title: ngMocks.formatText | ||
description: Documentation about ngMocks.formatText from ng-mocks library | ||
--- | ||
|
||
`ngMocks.formatText` is intended to normalize `textContent` in order to provide simpler text expectations in tests. | ||
|
||
`ngMocks.formatText` removes: | ||
- all html comments | ||
- all html tags | ||
- sequences of spaces, new lines, tabs will be replaced by a single space symbol | ||
|
||
`ngMocks.formatText` accepts: | ||
- a string | ||
- `HTMLElement`, `Text`, `Comment` | ||
- `DebugElement`, `DebugNode`, `ComponentFixture` | ||
- an array of them | ||
|
||
## dirty html | ||
|
||
a html like | ||
|
||
```html | ||
<div> | ||
<!-- binding1 --> | ||
<strong>header</strong> | ||
<!-- binding2 --> | ||
<ul> | ||
<li>1</li> | ||
<li>2</li> | ||
</ul> | ||
<!-- binding3 --> | ||
</div> | ||
``` | ||
|
||
becomes | ||
|
||
```html | ||
header 12 | ||
``` | ||
|
||
## ng-container | ||
|
||
A cool thing is that `ngMocks.formatText` uses [`ngMocks.crawl`](./crawl.md) under the hood | ||
and respects `ng-container`. | ||
|
||
so if we have a pointer to `ng-container`, we can assert its content. | ||
|
||
```html | ||
<div> | ||
< | ||
<ng-container block1>1</ng-container> | ||
& | ||
<ng-container block2>2</ng-container> | ||
> | ||
</div> | ||
``` | ||
|
||
```ts | ||
const div = ngMocks.find('div'); | ||
const block1 = ngMocks.reveal(div, ['block1']); | ||
const block2 = ngMocks.reveal(div, ['block2']); | ||
|
||
ngMocks.format(div); | ||
// returns | ||
// < 1 & 2 > | ||
|
||
ngMocks.formatHtml(block1); | ||
// returns | ||
// 1 | ||
ngMocks.formatHtml(block2); | ||
// returns | ||
// 2 | ||
``` |
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
29 changes: 29 additions & 0 deletions
29
libs/ng-mocks/src/lib/mock-helper/format/format-handler.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,29 @@ | ||
import nestedCheckChildren from '../crawl/nested-check-children'; | ||
|
||
import handleArray from './handle-array'; | ||
import isDebugNode from './is-debug-node'; | ||
import isFixture from './is-fixture'; | ||
import { FORMAT_SET, FORMAT_SINGLE } from './types'; | ||
|
||
export default (handlePrimitives: any) => (html: any, outer = false) => { | ||
const format = (value: Text | Comment | FORMAT_SINGLE | FORMAT_SET, innerOuter = false): any => { | ||
if (Array.isArray(value)) { | ||
return handleArray(format, value); | ||
} | ||
if (isFixture(value)) { | ||
return format(value.debugElement, outer); | ||
} | ||
const result = handlePrimitives(format, value, innerOuter); | ||
if (result !== undefined) { | ||
return result; | ||
} | ||
|
||
if (isDebugNode(value) && value.nativeNode.nodeName === '#comment') { | ||
return format(nestedCheckChildren(value), true); | ||
} | ||
|
||
return isDebugNode(value) ? format(value.nativeNode, innerOuter) : ''; | ||
}; | ||
|
||
return Array.isArray(html) ? html.map((item: any) => format(item, outer)) : format(html, outer); | ||
}; |
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 @@ | ||
export default (format: any, html: any) => { | ||
return format((html as any[]).map(item => format(item, true)).join('')); | ||
}; |
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 @@ | ||
export default (html: Text): string => html.nodeValue ?? html.textContent ?? html.wholeText; |
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,5 @@ | ||
import { MockedDebugNode } from '../../mock-render/types'; | ||
|
||
export default (value: any): value is MockedDebugNode => { | ||
return !!value?.nativeElement || !!value?.nativeNode; | ||
}; |
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,5 @@ | ||
import { MockedDebugNode } from '../../mock-render/types'; | ||
|
||
export default (value: any): value is { debugElement: MockedDebugNode } => { | ||
return !!value && typeof value === 'object' && value.debugElement !== undefined; | ||
}; |
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 @@ | ||
export default (value: any): value is HTMLElement => { | ||
return !!value && typeof value === 'object' && value.innerHTML !== undefined; | ||
}; |
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 @@ | ||
export default (value: any): value is Text => { | ||
return !!value && typeof value === 'object' && value.nodeName === '#text'; | ||
}; |
43 changes: 43 additions & 0 deletions
43
libs/ng-mocks/src/lib/mock-helper/format/mock-helper.format-html.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,43 @@ | ||
import formatHandler from './format-handler'; | ||
import handleText from './handle-text'; | ||
import isHtmlElement from './is-html-element'; | ||
import isText from './is-text'; | ||
|
||
const normalizeValue = (html: string | undefined): string => | ||
html | ||
? html | ||
.replace(new RegExp('\\s+', 'mg'), ' ') | ||
.replace(new RegExp('<!--.*?-->', 'mg'), '') | ||
.replace(new RegExp('\\s+', 'mg'), ' ') | ||
.replace(new RegExp('>\\s+<', 'mg'), '><') | ||
: ''; | ||
|
||
const normalizeText = (text: string): string => | ||
text | ||
.replace(new RegExp('&', 'mg'), '&') | ||
.replace(new RegExp('"', 'mg'), '"') | ||
.replace(new RegExp('<', 'mg'), '<') | ||
.replace(new RegExp('>', 'mg'), '>') | ||
// tslint:disable-next-line quotemark | ||
.replace(new RegExp("'", 'mg'), '''); | ||
|
||
const getElementValue = (element: HTMLElement, outer: boolean): string => | ||
outer ? element.outerHTML : element.innerHTML; | ||
|
||
const handlePrimitives = (format: any, value: any, outer: boolean): string | undefined => { | ||
if (typeof value === 'string' || value === undefined) { | ||
const result = normalizeValue(value); | ||
|
||
return outer ? result : result.trim(); | ||
} | ||
if (isHtmlElement(value)) { | ||
return format(getElementValue(value, outer)); | ||
} | ||
if (isText(value)) { | ||
return handlePrimitives(format, normalizeText(handleText(value)), outer); | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export default (() => formatHandler(handlePrimitives))(); |
30 changes: 30 additions & 0 deletions
30
libs/ng-mocks/src/lib/mock-helper/format/mock-helper.format-text.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,30 @@ | ||
import formatHandler from './format-handler'; | ||
import handleText from './handle-text'; | ||
import isHtmlElement from './is-html-element'; | ||
import isText from './is-text'; | ||
|
||
const normalizeValue = (html: string | undefined): string => (html ? html.replace(new RegExp('\\s+', 'mg'), ' ') : ''); | ||
|
||
const getElementValue = (element: HTMLElement, outer: boolean): string => { | ||
const value = element.textContent ?? ''; | ||
|
||
return outer ? value : value.trim(); | ||
}; | ||
|
||
const handlePrimitives = (format: any, value: any, outer: boolean): string | undefined => { | ||
if (typeof value === 'string' || value === undefined) { | ||
const result = normalizeValue(value); | ||
|
||
return outer ? result : result.trim(); | ||
} | ||
if (isHtmlElement(value)) { | ||
return format(getElementValue(value, outer)); | ||
} | ||
if (isText(value)) { | ||
return handlePrimitives(format, handleText(value), outer); | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export default (() => formatHandler(handlePrimitives))(); |
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 @@ | ||
export type FORMAT_SINGLE = string | HTMLElement | { nativeNode: any } | { nativeElement: any } | { debugElement: any }; | ||
export type FORMAT_SET = | ||
| string[] | ||
| HTMLElement[] | ||
| Array<{ nativeNode: any }> | ||
| Array<{ nativeElement: any }> | ||
| Array<{ debugElement: any }>; |
90 changes: 0 additions & 90 deletions
90
libs/ng-mocks/src/lib/mock-helper/mock-helper.format-html.ts
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
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
Oops, something went wrong.