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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ test-output/

.DS_Store
.marskman.toml

# IntelliJ project settings
.idea
# VSCode project settings
.vscode
10 changes: 6 additions & 4 deletions generators/opa5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default class extends Generator {
// prioritize manually passed parameter over config from file, as the latter is not up to date when subgenerator is composed
this.options.config.uimoduleName = this.options.uimoduleName
}
// remember tests were generated for post-processing to add the respective types to .tsconfig as well
this.options.config.enableTests = true
}

async writing() {
Expand All @@ -38,8 +40,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 +50,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 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
void onThe<%= viewName %>Page.iStartMyUIComponent({
componentConfig: {
name: "<%= uimoduleName %>",
async: true
}
});

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

// Cleanup
void onThe<%= viewName %>Page.iTeardownMyApp();
});
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"
});
}
}
8 changes: 5 additions & 3 deletions generators/qunit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class extends Generator {
// prioritize manually passed parameter over config from file, as the latter is not up to date when subgenerator is composed
this.options.config.uimoduleName = this.options.uimoduleName
}
// remember tests were generated for post-processing to add the respective types to .tsconfig as well
this.options.config.enableTests = true
}

async writing() {
Expand All @@ -35,9 +37,9 @@ 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")))
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);
});
5 changes: 5 additions & 0 deletions generators/uimodule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ export default class extends Generator {
tsconfigJson.compilerOptions.types = [ "@sapui5/types" ]
fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4))
}
if (this.options.config.enableTypescript && this.options.config.enableTests) {
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))
}
}

end() {
Expand Down
Loading