Skip to content

Commit

Permalink
feat(test): cypress shell test case
Browse files Browse the repository at this point in the history
Recorded shell test case refactor.

n/a
  • Loading branch information
jdre-c8y committed Jun 21, 2024
1 parent e61a1af commit 7605c07
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 12 deletions.
87 changes: 75 additions & 12 deletions cypress/e2e/datapoints-graph/datapoints-graph-1020.cy.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,88 @@
describe('datapoints-graph', { tags: '@1020' }, () => {
beforeEach(() => {
cy.intercept(
'/inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1&pageSize=1',
{ fixture: 'widgets/datapoints-graph/cockpit-dashboard.json' }
).as('cockpitDashboardConfig');
cy.useAuth('admin').login();
// TODO: make it configurable OR make sure folder is called 'cockpit'
cy.visit(
'/apps/cockpit/index.html?remotes=%7B"sag-pkg-community-plugins"%3A%5B"ExampleWidgetPluginModule"%2C"DatapointsGraphWidgetModule"%5D%7D#/'
);
// cy.visit('/apps/sag-pkg-community-plugins/#/');
cy.wait('@cockpitDashboardConfig', { timeout: 10_000 });

cy.request({
url: '/inventory/managedObjects',
method: 'POST',
headers: { Accept: 'application/json' },
body: {
c8y_IsDeviceGroup: {},
c8y_Notes: '',
name: 'e2eCopyGroup',
type: 'c8y_DeviceGroup',
},
}).then((groupRes) => {
cy.request({
url: `/inventory/managedObjects/${groupRes.body.id}/childAdditions`,
method: 'POST',
headers: {
'Content-Type':
'application/vnd.com.nsn.cumulocity.managedobject+json;',
Accept: 'application/json',
},
body: {
name: 'e2eDashboard',
[`c8y_Dashboard!group!${groupRes.body.id}`]: {},
c8y_Dashboard: {
name: 'e2eDashboard',
priority: 10000,
icon: 'th',
translateWidgetTitle: true,
children: {
'1': {
componentId: 'datapoints-graph',
classes: {
'alerts-overlay': false,
'card-dashboard': true,
'panel-title-regular': true,
map: true,
card: true,
},
_x: 0,
_y: 0,
id: '1',
title: 'Data points graph',
_width: 12,
config: {
datapoints: [],
displayDateSelection: false,
displayAggregationSelection: false,
widgetInstanceGlobalTimeContext: false,
canDecoupleGlobalTimeContext: false,
dateFrom: '2023-04-27T12:00:00.000Z',
dateTo: '2023-04-27T12:10:00.000Z',
interval: 'hours',
aggregation: null,
realtime: true,
yAxisSplitLines: false,
xAxisSplitLines: false,
},
_height: 6,
},
},
classes: { 'dashboard-theme-light': true },
c8y_IsNavigatorNode: null,
widgetClasses: { 'panel-title-regular': true },
},
},
}).then((dashboardRes) => {
cy.visit(
`/apps/cockpit/index.html?remotes=%7B"sag-pkg-community-plugins"%3A%5B"ExampleWidgetPluginModule"%2C"DatapointsGraphWidgetModule"%5D%7D#/group/${groupRes.body.id}/dashboard/${dashboardRes.body.id}`
);
});
});
});

it('view component should be present', () => {
cy.get('c8y-datapoints-graph-widget-view').should('exist');
cy.get('c8y-datapoints-graph-widget-view', { timeout: 10000 }).should(
'exist'
);
cy.get('c8y-charts').should('exist');
});

it('config component should be present', () => {
cy.get('[data-cy="c8y-widget-dashboard--edit-widgets"]')
cy.get('[data-cy="c8y-widget-dashboard--edit-widgets"]', { timeout: 10000 })
.should('be.visible')
.click();
cy.get(
Expand Down
38 changes: 38 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ declare global {
interceptCurrentTenant(): Chainable<void>;
interceptLoginOptions(): Chainable<void>;
interceptAppManifest(): Chainable<void>;
/**
* Below is the overwritten cy.request command with custom headers
* @param {object} originalFn - Original request fn
* @param {string} args - list of parameters needed for making cy.request
* @example cy.request('/inventory/managedObjects', 'POST', deviceObjCopy);
*/
request(originalFn: object, ...args: string[]): Chainable<any>;
}
}
}
Expand Down Expand Up @@ -95,3 +102,34 @@ Cypress.Commands.add('interceptAppManifest', () => {
}
).as('appManifest');
});

Cypress.Commands.overwrite('request', (originalFn, ...args) => {
let defaults;
cy.getCookie('XSRF-TOKEN').then((cookie) => {
if (!cookie) {
defaults = {};
} else if ((args[0] as any).headers) {
defaults = Cypress._.merge(args[0], {
headers: { 'X-XSRF-TOKEN': cookie.value },
});
} else {
defaults = {
headers: {
'X-XSRF-TOKEN': cookie.value,
'Content-Type': 'application/json',
},
};
}
let options = {} as any;
if (Cypress._.isObject(args[0])) {
options = Object.assign({}, args[0]);
} else if (args.length === 1) {
[options.url] = args;
} else if (args.length === 2) {
[options.url, options.method] = args;
} else if (args.length === 3) {
[options.url, options.method, options.body] = args;
}
return originalFn(Object.assign({}, defaults, options));
});
});

0 comments on commit 7605c07

Please sign in to comment.