-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
31 changed files
with
627 additions
and
62 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
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
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
125 changes: 125 additions & 0 deletions
125
src/blocks/transformers/temporaryInfo/DisplayTemporaryInfo.test.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,125 @@ | ||
/* | ||
* Copyright (C) 2022 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import DisplayTemporaryInfo from "@/blocks/transformers/temporaryInfo/DisplayTemporaryInfo"; | ||
import blockRegistry from "@/blocks/registry"; | ||
import { | ||
simpleInput, | ||
teapotBlock, | ||
testOptions, | ||
throwBlock, | ||
} from "@/runtime/pipelineTests/pipelineTestHelpers"; | ||
import { makePipelineExpression } from "@/runtime/expressionCreators"; | ||
import { DocumentRenderer } from "@/blocks/renderers/document"; | ||
import { getExampleBlockConfig } from "@/pageEditor/exampleBlockConfigs"; | ||
import { reducePipeline } from "@/runtime/reducePipeline"; | ||
import { BusinessError } from "@/errors/businessErrors"; | ||
import { | ||
PanelPayload, | ||
RendererError, | ||
TemporaryPanelEntry, | ||
} from "@/sidebar/types"; | ||
import { showTemporarySidebarPanel } from "@/contentScript/sidebarController"; | ||
|
||
jest.mock("@/background/messenger/api", () => { | ||
const actual = jest.requireActual("@/background/messenger/api"); | ||
return { | ||
...actual, | ||
getLoggingConfig: jest.fn().mockResolvedValue({ | ||
logValues: true, | ||
}), | ||
}; | ||
}); | ||
|
||
jest.mock("@/contentScript/sidebarController", () => ({ | ||
ensureSidebar: jest.fn(), | ||
showTemporarySidebarPanel: jest.fn(), | ||
})); | ||
|
||
jest.mock("@/blocks/transformers/temporaryInfo/temporaryPanelProtocol", () => ({ | ||
waitForTemporaryPanel: jest.fn(), | ||
stopWaitingForTemporaryPanels: jest.fn(), | ||
})); | ||
|
||
describe("DisplayTemporaryInfo", () => { | ||
const displayTemporaryInfoBlock = new DisplayTemporaryInfo(); | ||
const renderer = new DocumentRenderer(); | ||
|
||
beforeEach(() => { | ||
blockRegistry.clear(); | ||
blockRegistry.register( | ||
teapotBlock, | ||
throwBlock, | ||
renderer, | ||
displayTemporaryInfoBlock | ||
); | ||
}); | ||
|
||
test("it returns payload", async () => { | ||
const config = getExampleBlockConfig(renderer.id); | ||
const pipeline = { | ||
id: displayTemporaryInfoBlock.id, | ||
config: { | ||
title: "Test Temp Panel", | ||
body: makePipelineExpression([{ id: renderer.id, config }]), | ||
}, | ||
}; | ||
let payload: PanelPayload; | ||
(showTemporarySidebarPanel as jest.Mock).mockImplementation( | ||
(entry: TemporaryPanelEntry) => { | ||
payload = entry.payload; | ||
} | ||
); | ||
|
||
await reducePipeline(pipeline, simpleInput({}), testOptions("v3")); | ||
|
||
// Check structure for RendererPayload | ||
expect(payload).toHaveProperty("blockId", renderer.id); | ||
expect(payload).toHaveProperty("args"); | ||
expect(payload).toHaveProperty("ctxt"); | ||
}); | ||
|
||
test("it returns error", async () => { | ||
const message = "display info test error"; | ||
|
||
const pipeline = { | ||
id: displayTemporaryInfoBlock.id, | ||
config: { | ||
title: "Test Temp Panel", | ||
body: makePipelineExpression([ | ||
{ id: throwBlock.id, config: { message } }, | ||
{ id: renderer.id, config: getExampleBlockConfig(renderer.id) }, | ||
]), | ||
}, | ||
}; | ||
|
||
let payload: PanelPayload; | ||
(showTemporarySidebarPanel as jest.Mock).mockImplementation( | ||
(entry: TemporaryPanelEntry) => { | ||
payload = entry.payload; | ||
} | ||
); | ||
|
||
await reducePipeline(pipeline, simpleInput({}), testOptions("v3")); | ||
|
||
// Check structure for RendererError | ||
expect(payload).toHaveProperty("error"); | ||
const error = payload as RendererError; | ||
const errorMessage = (error.error as BusinessError).message; | ||
expect(errorMessage).toStrictEqual(message); | ||
}); | ||
}); |
123 changes: 123 additions & 0 deletions
123
src/blocks/transformers/temporaryInfo/DisplayTemporaryInfo.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,123 @@ | ||
/* | ||
* Copyright (C) 2022 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Transformer } from "@/types"; | ||
import { uuidv4, validateRegistryId } from "@/types/helpers"; | ||
import { BlockArg, BlockOptions, Schema } from "@/core"; | ||
import { PipelineExpression } from "@/runtime/mapArgs"; | ||
import { expectContext } from "@/utils/expectContext"; | ||
import { | ||
ensureSidebar, | ||
hideTemporarySidebarPanel, | ||
PANEL_HIDING_EVENT, | ||
showTemporarySidebarPanel, | ||
} from "@/contentScript/sidebarController"; | ||
import { PanelPayload } from "@/sidebar/types"; | ||
import { | ||
waitForTemporaryPanel, | ||
stopWaitingForTemporaryPanels, | ||
} from "@/blocks/transformers/temporaryInfo/temporaryPanelProtocol"; | ||
|
||
class DisplayTemporaryInfo extends Transformer { | ||
static BLOCK_ID = validateRegistryId("@pixiebrix/display"); | ||
defaultOutputKey = "infoOutput"; | ||
|
||
constructor() { | ||
super( | ||
DisplayTemporaryInfo.BLOCK_ID, | ||
"Display Temporary Information", | ||
"A display title for the information, shown in the tab name" | ||
); | ||
} | ||
|
||
inputSchema: Schema = { | ||
type: "object", | ||
properties: { | ||
title: { | ||
type: "string", | ||
description: "A display title for the temporary document", | ||
}, | ||
body: { | ||
$ref: "https://app.pixiebrix.com/schemas/pipeline#", | ||
description: "The render pipeline for the temporary document", | ||
}, | ||
}, | ||
required: ["body"], | ||
}; | ||
|
||
async transform( | ||
{ | ||
title, | ||
body: bodyPipeline, | ||
}: BlockArg<{ | ||
title: string; | ||
body: PipelineExpression; | ||
}>, | ||
{ | ||
logger: { | ||
context: { extensionId }, | ||
}, | ||
ctxt, | ||
runPipeline, | ||
runRendererPipeline, | ||
}: BlockOptions | ||
): Promise<unknown> { | ||
expectContext("contentScript"); | ||
|
||
const nonce = uuidv4(); | ||
const controller = new AbortController(); | ||
|
||
await ensureSidebar(); | ||
|
||
const payload = (await runRendererPipeline(bodyPipeline?.__value__ ?? [], { | ||
key: "body", | ||
counter: 0, | ||
})) as PanelPayload; | ||
|
||
showTemporarySidebarPanel({ | ||
extensionId, | ||
nonce, | ||
heading: title, | ||
payload, | ||
}); | ||
|
||
window.addEventListener( | ||
PANEL_HIDING_EVENT, | ||
() => { | ||
controller.abort(); | ||
}, | ||
{ | ||
signal: controller.signal, | ||
} | ||
); | ||
|
||
controller.signal.addEventListener("abort", () => { | ||
hideTemporarySidebarPanel(nonce); | ||
void stopWaitingForTemporaryPanels([nonce]); | ||
}); | ||
|
||
try { | ||
await waitForTemporaryPanel(nonce); | ||
} finally { | ||
controller.abort(); | ||
} | ||
|
||
return {}; | ||
} | ||
} | ||
|
||
export default DisplayTemporaryInfo; |
Oops, something went wrong.