Skip to content

Commit

Permalink
Improve Coverage (#540)
Browse files Browse the repository at this point in the history
* feedhandler: overcome lint warning

* test(WritableStream): cover else branch

* test(Parser): cover (open-implies-close case of) `onclosetag` else branch with non-`br` void close tag in non-XML mode

* refactor: improve else coverage by avoiding `openImpliesClose` optional chaining operator (check for existence already made upon entering block)

* refactor: improve coverage by avoiding null check for `endIndex` (since by calling `_updatePosition`, it will never be `null`). Cast endIndex as number to avoid TS believing it could be null.

* test(index): check APIs

* Move test case to event

Co-authored-by: Felix Böhm <me@feedic.com>
  • Loading branch information
brettz9 and fb55 committed Aug 24, 2020
1 parent 50f45c9 commit 6d8a2ff
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/FeedHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function addConditionally<T>(
recurse = false
) {
const tmp = fetch(what, where, recurse);
if (tmp) obj[prop] = tmp as any;
if (tmp) obj[prop] = tmp as unknown as T[keyof T];
}

function isValidFeed(value: string) {
Expand Down
6 changes: 2 additions & 4 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ export class Parser extends EventEmitter {
//Tokenizer event handlers
ontext(data: string) {
this._updatePosition(1);
if (this.endIndex !== null) {
this.endIndex--;
}
(this.endIndex as number)--;
this._cbs.ontext?.(data);
}

Expand All @@ -242,7 +240,7 @@ export class Parser extends EventEmitter {
) {
for (
let el;
openImpliesClose[name]?.has(
openImpliesClose[name].has(
(el = this._stack[this._stack.length - 1])
);
this.onclosetag(el)
Expand Down
1 change: 1 addition & 0 deletions src/WritableStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe("WritableStream", () => {

stream.write(Buffer.from([0xe2, 0x82]));
stream.write(Buffer.from([0xac]));
stream.write('');
stream.end();

expect(ontext).toBeCalledWith("€");
Expand Down
33 changes: 33 additions & 0 deletions src/__fixtures__/Events/35-non-br-void-close-tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "open-implies-close case of (non-br) void close tag in non-XML mode",
"options": {
"parser": { "lowerCaseAttributeNames": true }
},
"html": "<select><input></select>",
"expected": [
{
"event": "opentagname",
"data": ["select"]
},
{
"event": "opentag",
"data": ["select", {}]
},
{
"event": "closetag",
"data": ["select"]
},
{
"event": "opentagname",
"data": ["input"]
},
{
"event": "opentag",
"data": ["input", {}]
},
{
"event": "closetag",
"data": ["input"]
}
]
}
12 changes: 11 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { parseDOM, createDomStream } from ".";
import {
parseDOM, createDomStream, DomHandler, DefaultHandler, RssHandler
} from ".";
import { FeedHandler } from "./FeedHandler";
import { Element } from "domhandler";

// Add an `attributes` prop to the Element for now, to make it possible for Jest to render DOM nodes.
Expand Down Expand Up @@ -33,4 +36,11 @@ describe("Index", () => {

domStream.end();
});

describe("API", () => {
it("should export the appropriate APIs", () => {
expect(RssHandler).toEqual(FeedHandler);
expect(DomHandler).toEqual(DefaultHandler);
});
});
});

0 comments on commit 6d8a2ff

Please sign in to comment.