Skip to content

Commit

Permalink
Add jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Oct 10, 2024
1 parent aa3f99a commit dfcfd18
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,41 @@ describe('start', () => {
expect(updatedIsCollapsed).toBe(!isCollapsed);
});
});

describe('getIsFeedbackBtnVisible$', () => {
it('should return false by default', async () => {
const { chrome, service } = await start();
const isCollapsed = await firstValueFrom(chrome.sideNav.getIsFeedbackBtnVisible$());
service.stop();
expect(isCollapsed).toBe(false);
});

it('should return "false" when the sidenav is collapsed', async () => {
const { chrome, service } = await start();

const isFeedbackBtnVisible$ = chrome.sideNav.getIsFeedbackBtnVisible$();
chrome.sideNav.setIsFeedbackBtnVisible(true); // Mark it as visible
chrome.sideNav.setIsCollapsed(true); // But the sidenav is collapsed

const isFeedbackBtnVisible = await firstValueFrom(isFeedbackBtnVisible$);
service.stop();
expect(isFeedbackBtnVisible).toBe(false);
});
});

describe('setIsFeedbackBtnVisible', () => {
it('should update the isFeedbackBtnVisible$ observable', async () => {
const { chrome, service } = await start();
const isFeedbackBtnVisible$ = chrome.sideNav.getIsFeedbackBtnVisible$();
const isFeedbackBtnVisible = await firstValueFrom(isFeedbackBtnVisible$);

chrome.sideNav.setIsFeedbackBtnVisible(!isFeedbackBtnVisible);

const updatedIsFeedbackBtnVisible = await firstValueFrom(isFeedbackBtnVisible$);
service.stop();
expect(updatedIsFeedbackBtnVisible).toBe(!isFeedbackBtnVisible);
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const createStartContractMock = () => {
setIsCollapsed: jest.fn(),
getPanelSelectedNode$: jest.fn(),
setPanelSelectedNode: jest.fn(),
getIsFeedbackBtnVisible$: jest.fn(),
setIsFeedbackBtnVisible: jest.fn(),
},
getBreadcrumbsAppendExtension$: jest.fn(),
setBreadcrumbsAppendExtension: jest.fn(),
Expand Down
54 changes: 54 additions & 0 deletions src/plugins/navigation/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,60 @@ describe('Navigation Plugin', () => {
});
});

describe('set feedback button visibility', () => {
it('should set the feedback button visibility to "true" when space solution is a known solution', async () => {
const { plugin, coreStart, unifiedSearch, cloud, spaces } = setup();

for (const solution of ['es', 'oblt', 'security']) {
spaces.getActiveSpace$ = jest
.fn()
.mockReturnValue(of({ solution } as Pick<Space, 'solution'>));
plugin.start(coreStart, { unifiedSearch, cloud, spaces });
await new Promise((resolve) => setTimeout(resolve));
expect(coreStart.chrome.sideNav.setIsFeedbackBtnVisible).toHaveBeenCalledWith(true);
coreStart.chrome.sideNav.setIsFeedbackBtnVisible.mockReset();
}
});

it('should set the feedback button visibility to "false" for deployment in trial', async () => {
const { plugin, coreStart, unifiedSearch, cloud: cloudStart, spaces } = setup();
const coreSetup = coreMock.createSetup();
const cloudSetup = cloudMock.createSetup();
cloudSetup.trialEndDate = new Date(Date.now() + 1000 * 60 * 60 * 24 * 30); // 30 days from now
plugin.setup(coreSetup, { cloud: cloudSetup });

for (const solution of ['es', 'oblt', 'security']) {
spaces.getActiveSpace$ = jest
.fn()
.mockReturnValue(of({ solution } as Pick<Space, 'solution'>));
plugin.start(coreStart, { unifiedSearch, cloud: cloudStart, spaces });
await new Promise((resolve) => setTimeout(resolve));
expect(coreStart.chrome.sideNav.setIsFeedbackBtnVisible).toHaveBeenCalledWith(false);
coreStart.chrome.sideNav.setIsFeedbackBtnVisible.mockReset();
}
});

it('should not set the feedback button visibility for classic or unknown solution', async () => {
const { plugin, coreStart, unifiedSearch, cloud, spaces } = setup();

for (const solution of ['classic', 'unknown', undefined]) {
spaces.getActiveSpace$ = jest.fn().mockReturnValue(of({ solution }));
plugin.start(coreStart, { unifiedSearch, cloud, spaces });
await new Promise((resolve) => setTimeout(resolve));
expect(coreStart.chrome.sideNav.setIsFeedbackBtnVisible).not.toHaveBeenCalled();
coreStart.chrome.sideNav.setIsFeedbackBtnVisible.mockReset();
}
});

it('should not set the feedback button visibility when on serverless', async () => {
const { plugin, coreStart, unifiedSearch, cloud } = setup({ buildFlavor: 'serverless' });

plugin.start(coreStart, { unifiedSearch, cloud });
await new Promise((resolve) => setTimeout(resolve));
expect(coreStart.chrome.sideNav.setIsFeedbackBtnVisible).not.toHaveBeenCalled();
});
});

describe('isSolutionNavEnabled$', () => {
it('should be off if spaces plugin not available', async () => {
const { plugin, coreStart, unifiedSearch } = setup();
Expand Down

0 comments on commit dfcfd18

Please sign in to comment.