Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Hailong Cui <[email protected]>
  • Loading branch information
Hailong-am committed Sep 2, 2024
1 parent 40487cb commit 6275d4e
Show file tree
Hide file tree
Showing 5 changed files with 2,459 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () {
Object {
"newVersion": "4.0.1",
"prevVersion": "4.0.0",
"userLevel": false,
},
],
]
Expand Down
82 changes: 76 additions & 6 deletions src/core/server/ui_settings/ui_settings_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { createOrUpgradeSavedConfigMock } from './create_or_upgrade_saved_config

import { SavedObjectsClient } from '../saved_objects';
import { savedObjectsClientMock } from '../saved_objects/service/saved_objects_client.mock';
import { UiSettingsClient } from './ui_settings_client';
import { CURRENT_USER, UiSettingsClient } from './ui_settings_client';
import { CannotOverrideError } from './ui_settings_errors';

const logger = loggingSystemMock.create().get();
Expand Down Expand Up @@ -105,6 +105,22 @@ describe('ui settings', () => {
});
});

it('updates several values in one operation includes user level settings', async () => {
const value = chance.word();
const defaults = { key: { value, scope: 'user' } };
const { uiSettings, savedObjectsClient } = setup({ defaults });
await uiSettings.setMany({ one: 'value', another: 'val', key: 'value' });

expect(savedObjectsClient.update).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.update).toHaveBeenCalledWith(TYPE, ID, {
one: 'value',
another: 'val',
});
expect(savedObjectsClient.update).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`, {
key: 'value',
});
});

it('automatically creates the savedConfig if it is missing', async () => {
const { uiSettings, savedObjectsClient, createOrUpgradeSavedConfig } = setup();
savedObjectsClient.update
Expand Down Expand Up @@ -357,8 +373,9 @@ describe('ui settings', () => {
const { uiSettings, savedObjectsClient } = setup();
await uiSettings.getUserProvided();

expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, ID);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`);
});

it('returns user configuration', async () => {
Expand Down Expand Up @@ -410,6 +427,9 @@ describe('ui settings', () => {
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
]
`);
});
Expand All @@ -419,16 +439,21 @@ describe('ui settings', () => {
savedObjectsClient.get = jest
.fn()
.mockRejectedValueOnce(SavedObjectsClient.errors.createGenericNotFoundError())
.mockResolvedValueOnce({ attributes: {} })
.mockRejectedValueOnce(SavedObjectsClient.errors.createGenericNotFoundError())
.mockResolvedValueOnce({ attributes: {} });

expect(await uiSettings.getUserProvided()).toStrictEqual({});

expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(4);

expect(createOrUpgradeSavedConfig).toHaveBeenCalledTimes(1);
expect(createOrUpgradeSavedConfig).toHaveBeenCalledTimes(2);
expect(createOrUpgradeSavedConfig).toHaveBeenCalledWith(
expect.objectContaining({ handleWriteErrors: true })
);
expect(createOrUpgradeSavedConfig).toHaveBeenCalledWith(
expect.objectContaining({ handleWriteErrors: true, userLevel: true })
);
});

it('returns result of savedConfig creation in case of notFound error', async () => {
Expand Down Expand Up @@ -518,8 +543,9 @@ describe('ui settings', () => {
const opensearchDocSource = {};
const { uiSettings, savedObjectsClient } = setup({ opensearchDocSource });
await uiSettings.getAll();
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, ID);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`);
});

it('returns defaults when opensearch doc is empty', async () => {
Expand Down Expand Up @@ -552,6 +578,9 @@ describe('ui settings', () => {
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
]
`);
});
Expand Down Expand Up @@ -599,6 +628,43 @@ describe('ui settings', () => {
bar: 'user-provided',
});
});

// verify user level settings override global
it(`user level values will override global settings`, async () => {
const defaults = {
foo: {
value: 'default',
},
bar: {
value: 'default',
scope: 'user',
},
};

const { uiSettings, savedObjectsClient } = setup({ defaults });

savedObjectsClient.get.mockImplementation((_type, id, _options) => {
if (id === `${CURRENT_USER}_${ID}`) {
return Promise.resolve({
attributes: {
bar: 'my personal value',
},
} as any);
} else {
return Promise.resolve({
attributes: {
foo: 'default1',
bar: 'global value',
},
} as any);
}
});

expect(await uiSettings.getAll()).toStrictEqual({
foo: 'default1',
bar: 'my personal value',
});
});
});

describe('#getDefault()', () => {
Expand Down Expand Up @@ -626,8 +692,9 @@ describe('ui settings', () => {
const { uiSettings, savedObjectsClient } = setup({ opensearchDocSource });
await uiSettings.get('any');

expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, ID);
expect(savedObjectsClient.get).toHaveBeenCalledWith(TYPE, `${CURRENT_USER}_${ID}`);
});

it(`returns the promised value for a key`, async () => {
Expand Down Expand Up @@ -722,6 +789,9 @@ describe('ui settings', () => {
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
Array [
"Ignore invalid UiSettings value. ValidationError: [validation [id]]: expected value of type [number] but got [string].",
],
]
`);
});
Expand Down
Loading

0 comments on commit 6275d4e

Please sign in to comment.