Files
baserow/web-frontend/test/unit/dashboard/components/summaryWidgetSettings.spec.js

299 lines
8.9 KiB
JavaScript

import { DOMWrapper, flushPromises } from '@vue/test-utils'
import { TestApp } from '@baserow/test/helpers/testApp'
import WidgetSettings from '@baserow/modules/dashboard/components/widget/WidgetSettings'
import AggregateRowsDataSourceForm from '@baserow/modules/dashboard/components/data_source/AggregateRowsDataSourceForm'
let testApp
beforeEach(() => {
testApp = new TestApp()
})
afterEach(() => testApp.afterEach())
test('summary sources expose the shared filter control and save permanent conditions', async () => {
const source = {
id: 4,
integration_id: 10,
type: 'local_baserow_aggregate_rows',
table_id: 2,
field_id: 3,
aggregation_type: 'sum',
view_id: null,
filters: [],
filter_groups: [],
filter_type: 'AND',
}
const widget = {
id: 5,
type: 'summary',
title: 'Total',
description: '',
data_source_id: 4,
}
const store = testApp.store
store.commit('dashboardApplication/SET_DASHBOARD_ID', 1)
store.commit('dashboardApplication/SET_DATA_SOURCES', [source])
store.commit('dashboardApplication/SET_WIDGETS', [widget])
vi.spyOn(store.$client, 'get').mockResolvedValue({
data: [
{ id: 7, name: 'Name', type: 'text', table_id: 2 },
{
id: 3,
name: 'Amount',
type: 'number',
table_id: 2,
number_decimal_places: 0,
},
],
})
const dispatch = vi.spyOn(store, 'dispatch').mockResolvedValue()
const wrapper = await testApp.mount(WidgetSettings, {
props: { dashboard: { id: 1 }, widget },
global: { stubs: { Teleport: false } },
})
await flushPromises()
// Integration metadata can arrive after widgets and data sources.
store.commit('dashboardApplication/ADD_INTEGRATION', {
id: 10,
context_data: {
databases: [
{
id: 1,
name: 'Database',
tables: [{ id: 2, name: 'Rows' }],
views: [],
},
],
},
})
await flushPromises()
expect(wrapper.html()).toMatchSnapshot()
const filterButton = wrapper.find(
'.service-form__filter-buttons .service-form__refinement-chip'
)
expect(filterButton.text()).toBe('serviceRefinementForms.filterTabTitle - 0')
await filterButton.trigger('click')
const body = new DOMWrapper(document.body)
const context = body.find('.service-form__context--filter')
expect(context.text()).toContain('serviceRefinementForms.noFiltersTitle')
await context
.findAll('button')
.find(
(button) =>
button.text() === 'localBaserowTableServiceConditionalForm.addFilter'
)
.trigger('click')
await flushPromises()
const valueInput = context.find(
'input[placeholder="localBaserowTableServiceConditionalForm.textFilterInputPlaceholder"]'
)
await valueInput.setValue('Alpha')
await valueInput.trigger('keydown', { key: 'Enter' })
await flushPromises()
expect(dispatch).toHaveBeenCalledWith(
'dashboardApplication/updateDataSource',
{
dataSourceId: 4,
values: expect.objectContaining({
filters: [
expect.objectContaining({
field: 7,
type: 'equal',
value: expect.objectContaining({ formula: 'Alpha', mode: 'raw' }),
}),
],
}),
}
)
expect(context.find('[contenteditable=true]').exists()).toBe(false)
store.commit('dashboardApplication/ADD_WIDGET', {
id: 9,
type: 'text_filter',
title: 'Country',
target_filters: [
{
target_widget_id: widget.id,
empty_behavior: 'apply_nothing',
group: {
filter_type: 'AND',
filter_groups: [],
filters: [
{
id: 10,
field: 7,
type: 'equal',
value: { formula: 'France', mode: 'raw' },
},
],
},
},
],
})
await flushPromises()
expect(filterButton.find('.service-form__refinement-count').text()).toBe('2')
const inherited = context.find('.dashboard-widget-filters__source')
expect(inherited.text()).toContain('Country')
expect(inherited.find('input[disabled]').element.value).toBe('France')
await valueInput.setValue('Beta')
await valueInput.trigger('keydown', { key: 'Enter' })
await flushPromises()
const updates = dispatch.mock.calls.filter(
([action]) => action === 'dashboardApplication/updateDataSource'
)
expect(updates.at(-1)[1].values.filters).toEqual([
expect.objectContaining({
field: 7,
value: expect.objectContaining({ formula: 'Beta' }),
}),
])
expect(inherited.find('input[disabled]').element.value).toBe('France')
const form = wrapper.findComponent(AggregateRowsDataSourceForm)
// The mocked save above does not update the store; acknowledge it before
// simulating subsequent realtime updates.
store.commit('dashboardApplication/UPDATE_DATA_SOURCE', {
dataSourceId: 4,
values: { filters: JSON.parse(JSON.stringify(form.vm.values.filters)) },
})
await flushPromises()
const provided = form.vm.$.provides
expect(provided.serviceFilterTarget.value).toMatchInlineSnapshot(`
{
"id": 4,
"kind": "dashboard_data_source",
}
`)
expect(provided.serviceFilters.hasUnsavedChanges()).toBe(false)
const savedFilters = [
{
id: 91,
field: 7,
type: 'equal',
group: null,
value: { formula: 'Kuma', mode: 'raw' },
},
]
store.commit('dashboardApplication/UPDATE_DATA_SOURCE', {
dataSourceId: 4,
values: { filters: savedFilters },
})
await flushPromises()
expect(form.vm.values.filters).toEqual(savedFilters)
expect(provided.serviceFilters.hasUnsavedChanges()).toBe(false)
form.vm.values.filters[0].value.formula = 'Unsaved edit'
await form.vm.$nextTick()
expect(provided.serviceFilters.hasUnsavedChanges()).toBe(true)
store.commit('dashboardApplication/UPDATE_DATA_SOURCE', {
dataSourceId: 4,
values: {
filters: [
{ ...savedFilters[0], value: { formula: 'Other tab', mode: 'raw' } },
],
},
})
await flushPromises()
expect(form.vm.values.filters[0].value.formula).toBe('Unsaved edit')
})
test('summary form keeps values saved elsewhere while it is open', async () => {
const source = {
id: 4,
integration_id: 10,
type: 'local_baserow_aggregate_rows',
table_id: 2,
field_id: 3,
aggregation_type: 'sum',
view_id: null,
filters: [],
filter_groups: [],
filter_type: 'AND',
}
const widget = {
id: 5,
type: 'summary',
title: 'Total',
description: '',
data_source_id: 4,
}
const store = testApp.store
store.commit('dashboardApplication/SET_DASHBOARD_ID', 1)
store.commit('dashboardApplication/SET_DATA_SOURCES', [source])
store.commit('dashboardApplication/SET_WIDGETS', [widget])
vi.spyOn(store.$client, 'get').mockResolvedValue({
data: [
{ id: 7, name: 'Name', type: 'text', table_id: 2 },
{
id: 3,
name: 'Amount',
type: 'number',
table_id: 2,
number_decimal_places: 0,
},
],
})
const dispatch = vi.spyOn(store, 'dispatch').mockResolvedValue()
const wrapper = await testApp.mount(WidgetSettings, {
props: { dashboard: { id: 1 }, widget },
global: { stubs: { Teleport: false } },
})
await flushPromises()
store.commit('dashboardApplication/ADD_INTEGRATION', {
id: 10,
context_data: {
databases: [
{
id: 1,
name: 'Database',
tables: [{ id: 2, name: 'Rows' }],
views: [],
},
],
},
})
await flushPromises()
const form = wrapper.findComponent(AggregateRowsDataSourceForm)
dispatch.mockClear()
// Another user, or an undo, changes the aggregation.
store.commit('dashboardApplication/UPDATE_DATA_SOURCE', {
dataSourceId: 4,
values: { aggregation_type: 'max' },
})
await flushPromises()
expect(form.vm.values.aggregation_type).toBe('max')
expect(
dispatch.mock.calls.filter(
([action]) => action === 'dashboardApplication/updateDataSource'
)
).toHaveLength(0)
// Editing the filters afterwards doesn't put the old aggregation back.
await wrapper
.find('.service-form__filter-buttons .service-form__refinement-chip')
.trigger('click')
const context = new DOMWrapper(document.body).find(
'.service-form__context--filter'
)
await context
.findAll('button')
.find(
(button) =>
button.text() === 'localBaserowTableServiceConditionalForm.addFilter'
)
.trigger('click')
await flushPromises()
const valueInput = context.find(
'input[placeholder="localBaserowTableServiceConditionalForm.textFilterInputPlaceholder"]'
)
await valueInput.setValue('Alpha')
await valueInput.trigger('keydown', { key: 'Enter' })
await flushPromises()
const updates = dispatch.mock.calls.filter(
([action]) => action === 'dashboardApplication/updateDataSource'
)
expect(updates.length).toBeGreaterThan(0)
updates.forEach(([, { values }]) => {
expect(values.aggregation_type).toBe('max')
})
})