Skip to content

Commit

Permalink
chore: add forcepublish option, fix filter expression check
Browse files Browse the repository at this point in the history
  • Loading branch information
gtk-grafana committed Dec 19, 2024
1 parent 2b93993 commit a170789
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
76 changes: 76 additions & 0 deletions packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,44 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
expect(evtHandler).not.toHaveBeenCalled();
});

it('Should not overwrite filterExpression on setState', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
applyMode: 'manual',
filters: [{ key: 'key1', operator: '=', value: 'val1' }],
filterExpression: '',
});

variable.activate();

const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.setState({ filters: variable.state.filters.slice(0), filterExpression: 'hello filter expression!' });

expect(evtHandler).not.toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual('hello filter expression!');
});

it('Should overwrite filterExpression on updateFilters', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
applyMode: 'manual',
filters: [{ key: 'key1', operator: '=', value: 'val1' }],
filterExpression: 'hello filter expression!',
});

variable.activate();

const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: variable.state.filters.slice(0) });

expect(evtHandler).toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual('key1="val1"');
});

it('updateFilters should not publish event when expr did not change', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
Expand All @@ -975,11 +1013,30 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
expect(evtHandler).not.toHaveBeenCalled();
});

it('updateFilters should publish event when expr did not change, but forcePublish is set', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
applyMode: 'manual',
filters: [{ key: 'key1', operator: '=', value: 'val1' }],
});

variable.activate();

const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: variable.state.filters.slice(0) }, { forcePublish: true });

expect(evtHandler).toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual('key1="val1"');
});

it('updateFilters should publish event on when expr did change', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
applyMode: 'manual',
filters: [{ key: 'key1', operator: '=', value: 'val1' }],
filterExpression: 'hello filter expression',
});

variable.activate();
Expand All @@ -993,6 +1050,25 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
});

it('updateFilters should not publish event when skip event is true', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
applyMode: 'manual',
filters: [{ key: 'key1', operator: '=', value: 'val1' }],
filterExpression: 'hello filter expression',
});

variable.activate();

const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: [{ key: 'key2', operator: '=', value: 'val1' }] }, { skipPublish: true });

expect(evtHandler).not.toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
});

it('updateFilters should not publish event on when expr did change, if skipPublish is true', () => {
const variable = new AdHocFiltersVariable({
datasource: { uid: 'hello' },
Expand Down
5 changes: 3 additions & 2 deletions packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,19 @@ export class AdHocFiltersVariable
update: Partial<AdHocFiltersVariableState>,
options?: {
skipPublish?: boolean;
forcePublish?: boolean;
}
): void {
let filterExpressionChanged = false;

if (update.filters && update.filters !== this.state.filters) {
if (update.filters && update.filters !== this.state.filters && !update.filterExpression) {
update.filterExpression = renderExpression(this.state.expressionBuilder, update.filters);
filterExpressionChanged = update.filterExpression !== this.state.filterExpression;
}

super.setState(update);

if (filterExpressionChanged && options?.skipPublish !== true) {
if ((filterExpressionChanged && options?.skipPublish !== true) || options?.forcePublish === true) {
this.publishEvent(new SceneVariableValueChangedEvent(this), true);
}
}
Expand Down

0 comments on commit a170789

Please sign in to comment.