Fix bug in CircuitManager

This bug was introduced by removing the parsing from
8fa4df9bda but not updating the tests. We
expect component and rendererId to always be numbers (not strings).

It looks like these tests are not currently running on the CI which was
how we were able to find the issue.
This commit is contained in:
Ryan Nowak 2019-07-15 16:06:28 -07:00
parent 94edcbf65b
commit 496249fee9
3 changed files with 41 additions and 21 deletions

File diff suppressed because one or more lines are too long

View File

@ -94,12 +94,10 @@ function getComponentStartComment(node: Node): StartComponentComment | undefined
rendererId: rendererId,
componentId: componentId,
};
} else {
throw new Error(`Found malformed start component comment at ${node.textContent}`);
}
} catch (error) {
throw new Error(`Found malformed start component comment at ${node.textContent}`);
}
throw new Error(`Found malformed start component comment at ${node.textContent}`);
}
}
}
@ -114,20 +112,19 @@ function getComponentEndComment(component: StartComponentComment, children: Node
}
const componentEndComment = /\W+M.A.C.Component:\W+(\d+)\W+$/;
const definition = componentEndComment.exec(node.textContent);
const rawComponentId = definition && definition[1];
if (!rawComponentId) {
const json = definition && definition[1];
if (!json) {
continue;
}
try {
const componentId = Number.parseInt(rawComponentId);
// The value is expected to be a JSON encoded number
const componentId = JSON.parse(json);
if (componentId === component.componentId) {
return { componentId, node: node as Comment, index: i };
} else {
throw new Error(`Found malformed end component comment at ${node.textContent}`);
}
} catch (error) {
throw new Error(`Found malformed end component comment at ${node.textContent}`);
}
throw new Error(`Found malformed end component comment at ${node.textContent}`);
}
throw new Error(`End component comment not found for ${component.node}`);
}

View File

@ -1,7 +1,6 @@
(global as any).DotNet = { attachReviver: jest.fn() };
import { discoverPrerenderedCircuits } from '../src/Platform/Circuits/CircuitManager';
import { NullLogger } from '../src/Platform/Logging/Loggers';
import { JSDOM } from 'jsdom';
describe('CircuitManager', () => {
@ -14,7 +13,7 @@ describe('CircuitManager', () => {
</head>
<body>
<header>Preamble</header>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"1"} -->
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":1} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 1 -->
<footer></footer>
@ -40,11 +39,11 @@ describe('CircuitManager', () => {
</head>
<body>
<header>Preamble</header>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"1"} -->
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":1} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 1 -->
<footer>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"2"} -->
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":2} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 2 -->
</footer>
@ -66,7 +65,31 @@ describe('CircuitManager', () => {
expect(second.componentId).toEqual(2);
});
it('discoverPrerenderedCircuits throws for malformed circuits', () => {
it('discoverPrerenderedCircuits throws for malformed circuits - improper nesting', () => {
const dom = new JSDOM(`<!doctype HTML>
<html>
<head>
<title>Page</title>
</head>
<body>
<header>Preamble</header>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":1} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 2 -->
<footer>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":2} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 1 -->
</footer>
</body>
</html>`);
expect(() => discoverPrerenderedCircuits(dom.window.document))
.toThrow();
});
it('discoverPrerenderedCircuits throws for malformed circuits - mixed string and int', () => {
const dom = new JSDOM(`<!doctype HTML>
<html>
<head>
@ -76,11 +99,11 @@ describe('CircuitManager', () => {
<header>Preamble</header>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"1"} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 2 -->
<footer>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"2"} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 1 -->
<footer>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":2} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 2 -->
</footer>
</body>
</html>`);
@ -97,11 +120,11 @@ describe('CircuitManager', () => {
</head>
<body>
<header>Preamble</header>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"1"} -->
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":1} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 1 -->
<footer>
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":"2","componentId":"2"} -->
<!-- M.A.C.Component: {"circuitId":"1234","rendererId":2,"componentId":2} -->
<p>Prerendered content</p>
<!-- M.A.C.Component: 2 -->
</footer>