Skip to content

Commit

Permalink
Add additional templating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-dingler committed Apr 7, 2022
1 parent 6969430 commit d21771b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 28 deletions.
4 changes: 2 additions & 2 deletions source/nodejs/adaptivecards-templating/src/template-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ export interface IEvaluationContext {
* The root data object the template will bind to. Expressions that refer to $root in the template payload
* map to this field. Initially, $data also maps to $root.
*/
$root: any;
$root?: any;
/**
* The host data object the template will bind to. Expressions that refer to $host in the template payload
* map to this field.
* map to this field. This allows a host process to supply additional context to the template.
*/
$host?: any;
}
Expand Down
105 changes: 79 additions & 26 deletions source/nodejs/tests/unit-tests/src/unit-tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as AdaptiveCards from "adaptivecards";
import * as ACData from "adaptivecards-templating";

var fs = require("fs");
import { readFileSync } from "fs";

describe("test Text property on TextBlock", () => {
it("UnitTest1", () => {
Expand All @@ -18,8 +17,30 @@ describe("test Text property on TextBlock", () => {
});
});

const helloOutput = {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Hello Adaptive Cards!"
}
]
};

const helloThemeOutput = {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Hello Adaptive Cards! You're using the Light theme!"
}
]
};

describe("Test Templating Library", () => {
it("BasicTemplate", () => {
it("BasicTemplateOnlyRoot", () => {
const templatePayload = {
"type": "AdaptiveCard",
"version": "1.0",
Expand All @@ -35,21 +56,29 @@ describe("Test Templating Library", () => {
"name": "Adaptive Cards"
};

const expectedOutput = {
runTest(templatePayload, helloOutput, root);
})

it("BasicTemplateOnlyHost", () => {
const templatePayload = {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Hello Adaptive Cards!"
}
"body": [
{
"type": "TextBlock",
"text": "Hello Adaptive Cards! You're using the ${$host.WindowsTheme} theme!"
}
]
};

const host = {
"WindowsTheme": "Light"
}

runTest(templatePayload, helloThemeOutput, undefined, host);
});

runTest(templatePayload, root, expectedOutput);
})

it("BasicTemplateWithHost", () => {
it("BasicTemplateBoth", () => {
const templatePayload = {
"type": "AdaptiveCard",
"version": "1.0",
Expand All @@ -67,36 +96,60 @@ describe("Test Templating Library", () => {
const host = {
"WindowsTheme": "Light"
}

const expectedOutput = {

runTest(templatePayload, helloThemeOutput, root, host);
});

it("HostAsString", () => {
const templatePayload = {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Hello ${$host}!"
}
]
};

const host = "Adaptive Cards";

runTest(templatePayload, helloOutput, undefined, host);
});

it("HostAsArray", () => {
const templatePayload = {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Hello Adaptive Cards! You're using the Light theme!"
}
"body": [
{
"type": "TextBlock",
"text": "Hello ${$host[0]}! You're using the ${$host[1].WindowsTheme} theme!"
}
]
};

runTest(templatePayload, root, expectedOutput, host);
const host = [ "Adaptive Cards", {"WindowsTheme": "Light"}]

runTest(templatePayload, helloThemeOutput, undefined, host);

});

it("ComplexTemplate", () => {
runTest(loadFile("template-test-resources/complex-template.json"),
loadFile("template-test-resources/complex-template.data.json"),
loadFile("template-test-resources/complex-template.output.json"));
loadFile("template-test-resources/complex-template.output.json"),
loadFile("template-test-resources/complex-template.data.json"));
});

it("ComplexTemplateWithHost", () => {
runTest(loadFile("template-test-resources/complex-template-host.json"),
loadFile("template-test-resources/complex-template-host.data.json"),
loadFile("template-test-resources/complex-template-host.output.json"),
loadFile("template-test-resources/complex-template-host.data.json"),
loadFile("template-test-resources/complex-template-host.host.json"));
});
});

function runTest(templatePayload: any, data: any, expectedOutput: any, host?: any) {
function runTest(templatePayload: any, expectedOutput: any, data?: any, host?: any) {
let template = new ACData.Template(templatePayload);

let context = {
Expand All @@ -110,6 +163,6 @@ function runTest(templatePayload: any, data: any, expectedOutput: any, host?: an
}

function loadFile(filePath: string) {
const dataObject = fs.readFileSync(filePath, "utf8");
const dataObject = readFileSync(filePath, "utf8");
return JSON.parse(dataObject);
}

0 comments on commit d21771b

Please sign in to comment.