-
-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: implement tests covering whitespaces handling
A few tests fail, confirming that the current behavior is not compliant with HTML specs. covers #118
- Loading branch information
Showing
2 changed files
with
62 additions
and
6 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 |
---|---|---|
@@ -1,8 +1,64 @@ | ||
import React from 'react'; | ||
import HTML from '../HTML'; | ||
import renderer from 'react-test-renderer'; | ||
import React from "react"; | ||
import { Text } from "react-native"; | ||
import HTML from "../HTML"; | ||
import renderer from "react-test-renderer"; | ||
|
||
test('renders correctly', () => { | ||
const tree = renderer.create(<HTML html="<span>Hello world</span>" />).toJSON(); | ||
function extractTextFromInstance(instance) { | ||
const textChunks = []; | ||
const texts = instance.findAllByType(Text); | ||
for (const text of texts) { | ||
if (typeof text.props.children === "string") { | ||
textChunks.push(text.props.children); | ||
} | ||
} | ||
return textChunks.join(""); | ||
} | ||
|
||
describe("HTML component", () => { | ||
it("renders correctly", () => { | ||
const tree = renderer | ||
.create(<HTML html="<span>Hello world</span>" />) | ||
.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
describe("regarding text space behavior", () => { | ||
it("collapses spaces when first text tag has a trailing space and the second starts with a space", () => { | ||
const testRenderer = renderer.create( | ||
<HTML html="<span>random </span> <span>text</span>" /> | ||
); | ||
const renderedText = extractTextFromInstance(testRenderer.root); | ||
expect(renderedText).toBe("random text"); | ||
}); | ||
it("preserves spaces when the first text tag has a trailing space and the current one doesn't start with a space", () => { | ||
const testRenderer = renderer.create( | ||
<HTML html="<b>bold </b><span>text</span>" /> | ||
); | ||
const renderedText = extractTextFromInstance(testRenderer.root); | ||
expect(renderedText).toBe("bold text"); | ||
}); | ||
it("preserves a space between two tags which don't contain spaces", () => { | ||
const testRenderer = renderer.create( | ||
<HTML html="<b>bold</b> <span>text</span>" /> | ||
); | ||
const renderedText = extractTextFromInstance(testRenderer.root); | ||
expect(renderedText).toBe("bold text"); | ||
}); | ||
}); | ||
describe("regarding text line returns behavior", () => { | ||
it("replaces newlines with spaces when neither in-between text chunks contain spaces", () => { | ||
const testRenderer = renderer.create( | ||
<HTML html="foo\nbar" /> | ||
); | ||
const renderedText = extractTextFromInstance(testRenderer.root); | ||
expect(renderedText).toBe("foo bar"); | ||
}); | ||
|
||
}); | ||
describe("regarding <pre> tags behavior", () => { | ||
it("preserves tabs, spaces and line returns", () => { | ||
const testRenderer = renderer.create(<HTML html="<pre>\t\n </pre>" />); | ||
const renderedText = extractTextFromInstance(testRenderer.root); | ||
expect(renderedText).toBe("\t\n "); | ||
}); | ||
}); | ||
}); |
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