Skip to content

Commit a82f676

Browse files
committed
Tests - Legend - Add emit test
1 parent 67b26cf commit a82f676

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/atoms/Legend.cy.js

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, reactive } from 'vue';
1+
import { defineComponent, h, reactive, ref } from 'vue';
22
import Legend from './Legend.vue';
33

44
describe('<Legend />', () => {
@@ -53,6 +53,7 @@ describe('<Legend />', () => {
5353
components: { Legend },
5454
setup() {
5555
const legendSet = legend
56+
const leg = ref(null);
5657

5758
const config = reactive({
5859
backgroundColor: '#fff',
@@ -62,10 +63,10 @@ describe('<Legend />', () => {
6263
cy: 'legend-container'
6364
});
6465

65-
return { legendSet, config };
66+
return { legendSet, config, leg };
6667
},
6768
template: `
68-
<Legend :legendSet="legendSet" :config="config">
69+
<Legend ref="leg" :legendSet="legendSet" :config="config">
6970
<template #legendTitle>
7071
<div data-cy="legend-title" style="width:100%;text-align:center;font-weight:bold;padding:1rem">Legend Title</div>
7172
</template>
@@ -74,13 +75,53 @@ describe('<Legend />', () => {
7475
</template>
7576
</Legend>
7677
`
77-
}));
78-
79-
cy.get('[data-cy="legend-title"]').should('exist').and('contain', 'Legend Title');
80-
cy.get('[data-cy="legend-item"]').as('items').should('have.length', 7);
81-
cy.get('@items').each((item, i) => {
82-
cy.wrap(item).as('item')
83-
cy.get('@item').should('contain', `${legend[i].name} - value:${legend[i].value}`)
78+
})).then(({ wrapper }) => {
79+
cy.get('[data-cy="legend-title"]').should('exist').and('contain', 'Legend Title');
80+
cy.get('[data-cy="legend-item"]').as('items').should('have.length', 7);
81+
cy.get('@items').each((item, i) => {
82+
cy.wrap(item).as('item')
83+
cy.get('@item').should('contain', `${legend[i].name} - value:${legend[i].value}`)
84+
})
8485
})
8586
});
86-
});
87+
88+
it('emits clickMarker with correct payload', () => {
89+
const config = {
90+
backgroundColor: '#fff',
91+
fontSize: 14,
92+
color: '#333',
93+
paddingBottom: 12,
94+
cy: 'legend-container',
95+
};
96+
97+
const handlers = { onMarker: () => {} };
98+
cy.spy(handlers, 'onMarker').as('onMarker');
99+
100+
cy.mount(Legend, {
101+
props: {
102+
legendSet: legend,
103+
config,
104+
onClickMarker: handlers.onMarker,
105+
},
106+
slots: {
107+
legendTitle: () =>
108+
h('div', {
109+
'data-cy': 'legend-title',
110+
style: 'width:100%;text-align:center;font-weight:bold;padding:1rem',
111+
}, 'Legend Title'),
112+
item: ({ legend: item }) =>
113+
h('div', { 'data-cy': 'legend-item' }, `${item.name} - value:${item.value}`),
114+
},
115+
});
116+
117+
cy.get('[data-cy="legend-marker"]').should('have.length', legend.length);
118+
cy.get('[data-cy="legend-marker"]').first().click();
119+
120+
cy.get('@onMarker').should('have.been.calledOnce');
121+
cy.get('@onMarker').its('firstCall.args.0').then((payload) => {
122+
expect(payload).to.have.property('i', 0);
123+
expect(payload.legend).to.include(legend[0]);
124+
});
125+
});
126+
127+
});

src/atoms/Legend.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function handleClick(legend, i) {
4444
<slot name="legendTitle" :titleSet="legendSet" />
4545
<div v-for="(legend, i) in legendSet" :key="`legend_${i}`"
4646
:class="{ 'vue-data-ui-legend-item': true, 'active': clickable }">
47-
<svg
47+
<svg
48+
data-cy="legend-marker"
4849
v-if="legend.shape"
4950
@click="handleClick(legend, i)"
5051
height="1em"

0 commit comments

Comments
 (0)