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

fix: JavaScript refactoring and fixes for test runs support #204

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private void validateExportParameters(ExportParams exportParams) {
if (exportParams.getDocumentType() == DocumentType.LIVE_DOC && exportParams.getProjectId() == null) {
throw new BadRequestException("Parameter 'projectId' should be provided");
}
if (exportParams.getLocationPath() == null) {
if (exportParams.getLocationPath() == null && exportParams.getDocumentType() != DocumentType.TEST_RUN) {
throw new BadRequestException("Parameter 'locationPath' should be provided");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export default class ExportContext {
this.projectId = getProjectId(scope);
this.locationPath = getPath(normalizedPolarionLocationHash, scope);

if (this.locationPath === "testrun") {
// if "testrun" or "testruns" is present return undefined
if (this.locationPath?.startsWith("testrun")) {
this.documentType = ExportParams.DocumentType.TEST_RUN;
this.locationPath = undefined;
}

this.urlQueryParameters = getQueryParams(searchParameters);
Expand Down Expand Up @@ -58,27 +60,32 @@ export default class ExportContext {

function getPath(locationHash, scope) {
if (scope) {
const pathPattern = /project\/[^/]+\/(wiki\/([^?#]+)|testrun)/;
const pathPattern = /project\/[^/]+\/(wiki\/([^?#]+)|testruns|testrun)/;
const pathMatch = pathPattern.exec(locationHash);
return pathMatch ? addDefaultSpaceIfRequired(pathMatch[2] || "testrun") : "";
const extractedPath = pathMatch ? (pathMatch[2] || pathMatch[1]) : undefined;
return pathMatch ? addDefaultSpaceIfRequired(extractedPath) : undefined;
} else {
const globalPathPattern = /wiki\/([^/?#]+)/;
const pathMatch = globalPathPattern.exec(locationHash);
return pathMatch ? addDefaultSpaceIfRequired(pathMatch[1]) : "";
return pathMatch ? addDefaultSpaceIfRequired(pathMatch[1]) : undefined;
}
}

function addDefaultSpaceIfRequired(extractedPath) {
if (!extractedPath) {
return "";
}
// if contains a '/' or is exactly 'testrun', return it as it is
if (extractedPath.includes("/") || extractedPath === "testrun") {
// if "testrun" or "testruns" is present return undefined
if (extractedPath.startsWith("testrun")) {
return extractedPath;
}
// if contains a '/' return it as it is
if (extractedPath.includes("/")) {
return extractedPath;
}
// otherwise, prepend '_default/' to the path
return `_default/${extractedPath}`;
}
};

function getQueryParams(searchParams) {
if (!searchParameters) {
Expand Down Expand Up @@ -116,13 +123,21 @@ export default class ExportContext {
}

getSpaceId() {
const pathParts = this.locationPath.split("/");
return pathParts && pathParts.length > 0 && pathParts[0];
if (this.locationPath?.includes("/")) {
const pathParts = this.locationPath.split("/");
return pathParts && pathParts.length > 0 && pathParts[0];
} else {
return undefined;
}
}

getDocumentName() {
const pathParts = this.locationPath.split("/");
return pathParts && pathParts.length > 1 && pathParts[1];
if (this.locationPath?.includes("/")) {
const pathParts = this.locationPath.split("/");
return pathParts && pathParts.length > 1 && pathParts[1];
} else {
return undefined;
}
}

toExportParams() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ const PdfExporter = {
},

openPopup: function (params) {
this.exportContext = params?.exportContext ? params.exportContext : new ExportContext();

this.hideAlerts();
this.loadFormData(params);
this.loadFormData();
const reportContext = this.exportContext.getDocumentType() === ExportParams.DocumentType.LIVE_REPORT || this.exportContext.getDocumentType() === ExportParams.DocumentType.TEST_RUN;
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.only-live-doc")
.forEach(propertyBlock => propertyBlock.style.display = (reportContext ? "none" : "flex"));
MicroModal.show('pdf-export-modal-popup');
},

loadFormData: function (params) {
this.exportContext = params?.exportContext ? params.exportContext : new ExportContext();

loadFormData: function () {
this.actionInProgress({inProgress: true, message: "Loading form data"});

Promise.all([
Expand Down
21 changes: 18 additions & 3 deletions src/test/js/ExportContextTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ describe('ExportContext Class', function () {

expect(exportContext.documentType).to.equal(ExportParams.DocumentType.TEST_RUN);
expect(exportContext.projectId).to.equal('elibrary');
expect(exportContext.locationPath).to.equal('testrun');
expect(exportContext.locationPath).to.be.undefined;
expect(exportContext.revision).to.be.undefined;
expect(exportContext.urlQueryParameters).to.deep.equal({ id: 'elibrary_20231026-163136654' });

expect(exportContext.getSpaceId()).to.equal('testrun');
expect(exportContext.getDocumentName()).to.be.false; // No document name for testrun
expect(exportContext.getSpaceId()).to.be.undefined;
expect(exportContext.getDocumentName()).to.be.undefined;
});

it('URL: #/project/elibrary/wiki/Reports/LiveReport%20with%20params?stringParameter=asd&workItemType=changerequest&yesnoParameter=yes', function () {
Expand All @@ -104,4 +104,19 @@ describe('ExportContext Class', function () {
expect(exportContext.getSpaceId()).to.equal('Reports');
expect(exportContext.getDocumentName()).to.equal('LiveReport with params');
});

it('URL: #/project/elibrary/testruns', function () {
const locationHash = "#/project/elibrary/testruns";
const exportContext = new ExportContext(ExportParams.DocumentType.LIVE_REPORT, locationHash);

expect(exportContext.documentType).to.equal(ExportParams.DocumentType.TEST_RUN);
expect(exportContext.projectId).to.equal('elibrary');
expect(exportContext.locationPath).to.be.undefined;
expect(exportContext.revision).to.be.undefined;
expect(exportContext.urlQueryParameters).to.be.undefined;

expect(exportContext.getSpaceId()).to.be.undefined;
expect(exportContext.getDocumentName()).to.be.undefined;
});

});
Loading