Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bfmatei committed Apr 26, 2024
1 parent 965f868 commit b223396
Showing 1 changed file with 96 additions and 14 deletions.
110 changes: 96 additions & 14 deletions packages/scenes/src/variables/groupby/GroupByVariable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,109 @@ describe('GroupByVariable', () => {
});
});

it('url sync works', async () => {
const { variable } = setupTest({
defaultOptions: [
{ text: 'a', value: 'a' },
{ text: 'b', value: 'b' },
],
});
describe('url sync', () => {
it('should work with default options', () => {
const { variable } = setupTest({
defaultOptions: [
{ text: 'a', value: 'a' },
{ text: 'b', value: 'b' },
],
});

expect(variable.state.value).toEqual('');

expect(variable.state.value).toEqual('');
act(() => {
variable.changeValueTo(['a']);
});

act(() => {
variable.changeValueTo(['a']);
expect(locationService.getLocation().search).toBe('?var-test=a');

act(() => {
locationService.push('/?var-test=a&var-test=b');
});

expect(variable.state.value).toEqual(['a', 'b']);

act(() => {
locationService.push('/?var-test=a&var-test=b&var-test=c');
});

expect(variable.state.value).toEqual(['a', 'b', 'c']);
});

expect(locationService.getLocation().search).toBe('?var-test=a');
it('should work with received options', async () => {
const { variable } = setupTest({
getTagKeysProvider: () => {
return Promise.resolve({
replace: true,
values: [
{ text: 'A', value: 'a' },
{ text: 'b', value: 'b' },
{ text: 'C', value: 'c' },
],
});
},
});

await act(async () => {
await lastValueFrom(variable.validateAndUpdate());
});

expect(variable.state.value).toEqual('');

act(() => {
variable.changeValueTo(['a']);
});

expect(locationService.getLocation().search).toBe('?var-test=a,A');

act(() => {
locationService.push('/?var-test=a,A&var-test=b');
});

act(() => {
locationService.push('/?var-test=a&var-test=b');
expect(variable.state.value).toEqual(['a', 'b']);

act(() => {
locationService.push('/?var-test=a,A&var-test=b&var-test=c');
});

expect(variable.state.value).toEqual(['a', 'b', 'c']);
});

expect(variable.state.value).toEqual(['a', 'b']);
it('should work with commas', async () => {
const { variable } = setupTest({
defaultOptions: [
{ text: 'A,something', value: 'a' },
{ text: 'B', value: 'b,something' },
],
});

expect(variable.state.value).toEqual('');

await act(async () => {
await lastValueFrom(variable.validateAndUpdate());
});

act(() => {
variable.changeValueTo(['a']);
});

expect(locationService.getLocation().search).toBe('?var-test=a,A__gfc__something');

act(() => {
locationService.push('/?var-test=a,A__gfc__something&var-test=b__gfc__something');
});

expect(variable.state.value).toEqual(['a', 'b,something']);

act(() => {
locationService.push(
'/?var-test=a,A__gfc__something&var-test=b__gfc__something&var-test=c__gfc__something,C__gfc__something'
);
});

expect(variable.state.value).toEqual(['a', 'b,something', 'c,something']);
});
});

it('Can override and replace getTagKeys', async () => {
Expand Down

0 comments on commit b223396

Please sign in to comment.