Skip to content

Commit c84fa36

Browse files
committed
VueUiSmiley add e2e component test
1 parent 25c4657 commit c84fa36

File tree

3 files changed

+172
-16
lines changed

3 files changed

+172
-16
lines changed

cypress/fixtures/smiley.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"dataset": {
3+
"rating": {
4+
"1": 1,
5+
"2": 1,
6+
"3": 1,
7+
"4": 1,
8+
"5": 30
9+
}
10+
},
11+
"config": {
12+
"readonly": false,
13+
"style": {
14+
"fontFamily": "inherit",
15+
"itemSize": 32,
16+
"backgroundColor": "#FFFFFF",
17+
"colors": {
18+
"activeReadonly": ["#e20001", "#ff9f03", "#ffd004", "#61c900", "#059f00"],
19+
"active": ["#e20001", "#ff9f03", "#ffd004", "#61c900", "#059f00"],
20+
"inactive": ["#e1e5e8","#e1e5e8","#e1e5e8","#e1e5e8","#e1e5e8"]
21+
},
22+
"icons": {
23+
"filled": false,
24+
"useGradient": true
25+
},
26+
"title": {
27+
"textAlign": "center",
28+
"fontSize": 20,
29+
"color": "#2D353C",
30+
"bold": true,
31+
"text": "Title",
32+
"offsetY": 6,
33+
"subtitle": {
34+
"fontSize": 14,
35+
"color": "#CCCCCC",
36+
"bold": false,
37+
"text": "Subtitle",
38+
"offsetY": 12
39+
}
40+
},
41+
"rating": {
42+
"show": true,
43+
"fontSize": 28,
44+
"bold": true,
45+
"roundingValue": 1,
46+
"position": "bottom",
47+
"offsetY": 0,
48+
"offsetX":0
49+
},
50+
"tooltip": {
51+
"show": true,
52+
"fontSize": 14,
53+
"offsetY": 0,
54+
"color":"#2D353C",
55+
"bold": true,
56+
"backgroundColor":"#FFFFFF",
57+
"borderColor": "#e1e5e8",
58+
"borderRadius": 4,
59+
"boxShadow":"0 6px 12px -6px rgba(0,0,0,0.2)"
60+
}
61+
}
62+
}
63+
}

src/components/vue-ui-smiley.cy.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import VueUiSmiley from './vue-ui-smiley.vue'
2+
3+
describe('<VueUiSmiley />', () => {
4+
beforeEach(function () {
5+
cy.fixture('smiley.json').as('fixture');
6+
cy.viewport(800, 160);
7+
});
8+
9+
function updateConfigInFixture(modifiedConfig) {
10+
cy.get('@fixture').then((fixture) => {
11+
const updatedFixture = { ...fixture, config: modifiedConfig };
12+
cy.wrap(updatedFixture).as('fixture');
13+
});
14+
}
15+
16+
it('renders with different config attributes', function () {
17+
cy.get('@fixture').then((fixture) => {
18+
cy.mount(VueUiSmiley, {
19+
props: {
20+
dataset: fixture.dataset,
21+
config: fixture.config
22+
}
23+
});
24+
25+
function calculateAverageRating(source) {
26+
if (source === null) return null;
27+
let totalSum = 0;
28+
let totalCount = 0;
29+
30+
for (const key in source) {
31+
const ratingValue = parseInt(key);
32+
const ratingCount = source[key];
33+
34+
totalSum += ratingValue * ratingCount;
35+
totalCount += ratingCount;
36+
}
37+
38+
if (totalCount === 0) {
39+
return 0;
40+
}
41+
42+
const averageRating = totalSum / totalCount;
43+
return averageRating;
44+
}
45+
46+
const staticRating = calculateAverageRating(fixture.dataset.rating).toFixed(fixture.config.style.rating.roundingValue);
47+
48+
cy.get(`[data-cy="smiley-title"]`)
49+
.should('exist')
50+
.contains('Title');
51+
52+
cy.get(`[data-cy="smiley-subtitle"]`)
53+
.should('exist')
54+
.contains('Subtitle');
55+
56+
cy.get(`[data-cy="smiley-position-bottom"]`)
57+
.should('exist')
58+
.contains(staticRating);
59+
60+
for (let i = 0; i < 5; i += 1) {
61+
cy.get(`[data-cy="smiley-item-${i}"]`)
62+
.should('exist')
63+
.click();
64+
65+
cy.get(`[data-cy="smiley-position-bottom"]`)
66+
.contains(`${i + 1}.0`)
67+
}
68+
69+
let modifiedConfig = {
70+
...fixture.config,
71+
readonly: true
72+
}
73+
74+
updateConfigInFixture(modifiedConfig);
75+
76+
cy.mount(VueUiSmiley, {
77+
props: {
78+
dataset: fixture.dataset,
79+
config: modifiedConfig
80+
}
81+
});
82+
83+
for (let i = 0; i < 5; i += 1) {
84+
cy.get(`[data-cy="smiley-item-${i}"]`)
85+
.trigger('mouseenter')
86+
87+
cy.get(`[data-cy="smiley-tooltip-${i}"]`)
88+
.should('exist')
89+
.contains(`${Object.keys(fixture.dataset.rating)[i]} : ${fixture.dataset.rating[i + 1]}`)
90+
}
91+
});
92+
});
93+
})

0 commit comments

Comments
 (0)