Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat add specific unit and opa5 test templates #78

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions generators/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default {
"@sap-ux/eslint-plugin-fiori-tools": "^0.2",
"@sap/approuter": "latest",
"@ui5/linter": "latest",
"@types/qunit": "2.5.4",
heimwege marked this conversation as resolved.
Show resolved Hide resolved
"mbt": "^1",
"rimraf": "latest",
"OpenUI5": "1.120.13",
Expand Down
15 changes: 11 additions & 4 deletions generators/opa5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "../helpers.js"
import path, { dirname } from "path"
import { fileURLToPath } from "url"
import dependencies from "../dependencies.js";
const __dirname = dirname(fileURLToPath(import.meta.url))

export default class extends Generator {
Expand Down Expand Up @@ -38,8 +39,8 @@ export default class extends Generator {

this.fs.copyTpl(
// for some reason this.templatePath() doesn't work here
path.join(__dirname, "templates/pages/View.js"),
this.destinationPath(`webapp/test/integration/pages/${this.options.config.viewName}.js`),
path.join(__dirname, `templates/pages/View.${this.options.config.enableTypescript ? "ts": "js"}`),
this.destinationPath(`webapp/test/integration/pages/${this.options.config.viewName}.${this.options.config.enableTypescript ? "ts": "js"}`),
{
viewName: this.options.config.viewName,
uimoduleName: this.options.config.uimoduleName,
Expand All @@ -48,8 +49,8 @@ export default class extends Generator {
)
this.fs.copyTpl(
// for some reason this.templatePath() doesn't work here
path.join(__dirname, "templates/Journey.js"),
this.destinationPath(`webapp/test/integration/${this.options.config.testName}Journey.js`),
path.join(__dirname, `templates/Journey.${this.options.config.enableTypescript ? "ts": "js"}`),
this.destinationPath(`webapp/test/integration/${this.options.config.testName}Journey.${this.options.config.enableTypescript ? "ts": "js"}`),
{
viewName: this.options.config.viewName,
uimoduleName: this.options.config.uimoduleName,
Expand All @@ -59,6 +60,12 @@ export default class extends Generator {

const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json")))
uimodulePackageJson.scripts["opa5"] = "fiori run --open test/opaTests.qunit.html"
if (this.options.config.enableTypescript) {
uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"]
const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json")))
tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" )
fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4))
}
fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4))
nicoschoenteich marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
28 changes: 28 additions & 0 deletions generators/opa5/templates/Journey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Opa5 from "sap/ui/test/Opa5";
import opaTest from "sap/ui/test/opaQunit";
import <%= viewName %>Page from "./pages/<%= viewName %>";

const onThe<%= viewName %>Page = new <%= viewName %>Page();

Opa5.extendConfig({
viewNamespace: "<%= uimoduleName %>.view",
autoWait: true
});

QUnit.module("<%= viewName %>");

opaTest("Should have correct title", function() {
// Arrangements
onThe<%= viewName %>Page.iStartMyUIComponent({
heimwege marked this conversation as resolved.
Show resolved Hide resolved
componentConfig: {
name: "<%= uimoduleName %>",
async: true
}
});

// Assertions
onThe<%= viewName %>Page.theTitleShouldBeCorrect();
heimwege marked this conversation as resolved.
Show resolved Hide resolved

// Cleanup
onThe<%= viewName %>Page.iTeardownMyApp();
heimwege marked this conversation as resolved.
Show resolved Hide resolved
});
25 changes: 25 additions & 0 deletions generators/opa5/templates/pages/View.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Opa5 from "sap/ui/test/Opa5";
import I18NText from "sap/ui/test/matchers/I18NText";

const viewName = "<%= viewName %>";

export default class <%= viewName %>Page extends Opa5 {
//Actions

//Assertions
theTitleShouldBeCorrect(this: Opa5) {
this.waitFor({
id: "page",
viewName,
matchers: new I18NText({
key: '<%- route === "" ? "title" : route %>',
propertyName: 'title',
parameters: '<%- route === "" ? uimoduleName : viewName %>'
}),
success: function() {
Opa5.assert.ok(true, "The page has the correct title");
},
errorMessage: "The page does not have the correct title"
});
}
}
13 changes: 10 additions & 3 deletions generators/qunit/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from "chalk"
import fs from "fs"
import Generator from "yeoman-generator"
import dependencies from "../dependencies.js"
import prompts from "./prompts.js"
import {
lookForParentUI5ProjectAndPrompt,
Expand Down Expand Up @@ -35,13 +36,19 @@ export default class extends Generator {

this.fs.copyTpl(
// for some reason this.templatePath() doesn't work here
path.join(__dirname, "templates/Test.js"),
this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.js`),
{ testName: this.options.config.testName }
path.join(__dirname, `templates/Test.${this.options.config.enableTypescript ? "ts": "js"}`),
this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.${this.options.config.enableTypescript ? "ts": "js"}`),
{testName: this.options.config.testName}
)

const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json")))
uimodulePackageJson.scripts["qunit"] = "fiori run --open test/unitTests.qunit.html"
if (this.options.config.enableTypescript) {
uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"]
const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json")))
tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" )
fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4))
}
fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4))
nicoschoenteich marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
6 changes: 6 additions & 0 deletions generators/qunit/templates/Test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
QUnit.module("<%= testName %> Test", {});

QUnit.test("It is just true", assert => {
// Assert
assert.strictEqual(true, true);
});