Skip to content

Commit

Permalink
Issue #ED-0000 test: updated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vpPavithra committed Jul 4, 2024
1 parent a95cd66 commit 63ad3f0
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ describe('SbGenericPopoverComponent', () => {
};

const mockPlatform: Partial<Platform> = {};
let subscribeWithPriorityCallback;
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((val, callback) => {
subscribeWithPriorityCallback = callback;
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;
const mockUtilityService: Partial<UtilityService> = {};
const mockLocation: Partial<Location> = {
back: jest.fn()
Expand All @@ -38,60 +47,101 @@ describe('SbGenericPopoverComponent', () => {
expect(groupGuideLinesPopoverComponent).toBeTruthy();
});

it('should subscribe to back button and events subscription', () => {
it('should subscribe to back button and events subscription', (done) => {
// arrange
const subscribeWithPriorityData = jest.fn((_, fn) => fn());
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((_, fn) => {
setTimeout(() => {
fn();
});
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;

const unsubscribeFn = jest.fn();
groupGuideLinesPopoverComponent.backButtonFunc = {
unsubscribe: unsubscribeFn,
} as any;
mockCommonUtilService.getAppName = jest.fn(() => 'sunbird');
mockCommonUtilService.getAppName = jest.fn(() => Promise.resolve('sunbird'));
groupGuideLinesPopoverComponent.shouldUpdateUserLevelGroupTnc = false
// act
groupGuideLinesPopoverComponent.ngOnInit();
// assert
setTimeout(() => {
expect(mockPopOverController.dismiss).toHaveBeenCalledWith({ isLeftButtonClicked: null });
expect(unsubscribeFn).toHaveBeenCalled();
// expect(mockPopOverController.dismiss).toHaveBeenCalledWith({ isLeftButtonClicked: null });
done()
});
});

it('should subscribe to back button and events subscription', () => {
it('should subscribe to back button and events subscription', (done) => {
// arrange
const subscribeWithPriorityData = jest.fn((_, fn) => fn());
let subscribeWithPriorityCallback;
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((_, fn) => {
setTimeout(() => {
fn();
});
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;

const unsubscribeFn = jest.fn();
groupGuideLinesPopoverComponent.backButtonFunc = {
unsubscribe: unsubscribeFn,
} as any;
mockCommonUtilService.getAppName = jest.fn(() => 'sunbird');
mockCommonUtilService.getAppName = jest.fn(() => Promise.resolve('sunbird'));
mockLocation.back = jest.fn()
groupGuideLinesPopoverComponent.shouldUpdateUserLevelGroupTnc = true
// act
groupGuideLinesPopoverComponent.ngOnInit();
// assert
setTimeout(() => {
expect(mockLocation.back).toHaveBeenCalled()
expect(unsubscribeFn).toHaveBeenCalled();
expect(mockBackBtnFunc.unsubscribe).toHaveBeenCalled();
done()
});
});

it('should unsubscribe to back button and events on ngOnDestroy', () => {
it('should subscribe to back button and events subscription', (done) => {
// arrange
groupGuideLinesPopoverComponent.backButtonFunc = {
unsubscribe: jest.fn(),
let subscribeWithPriorityCallback;
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((_, fn) => {
setTimeout(() => {
fn();
});
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;
mockCommonUtilService.getAppName = jest.fn(() => Promise.resolve('sunbird'));
mockLocation.back = jest.fn()
groupGuideLinesPopoverComponent.shouldUpdateUserLevelGroupTnc = false
// act
groupGuideLinesPopoverComponent.ngOnInit();
// assert
setTimeout(() => {
expect(mockBackBtnFunc.unsubscribe).toHaveBeenCalled();
done()
});
});

it('should unsubscribe to back button and events on ngOnDestroy', (done) => {
// arrange
let subscribeWithPriorityCallback;
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((_, fn) => {
setTimeout(() => {
fn();
});
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;
// act
groupGuideLinesPopoverComponent.ngOnDestroy();
// assert
// expect(groupGuideLinesPopoverComponent.selectedContents).toEqual(mockEventsResponse);
expect(groupGuideLinesPopoverComponent.backButtonFunc.unsubscribe).toHaveBeenCalled();
setTimeout(() => {
expect(groupGuideLinesPopoverComponent.backButtonFunc.unsubscribe).toHaveBeenCalled();
done()
}, 0);
});

it('should dismiss the popup on closePopOver', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('SbGenericPopoverComponent', () => {
};

const mockPlatform: Partial<Platform> = {
backButton: {
subscribeWithPriority: jest.fn()
} as Partial<BackButtonEmitter> as BackButtonEmitter,
};

beforeAll(() => {
Expand All @@ -36,15 +39,18 @@ describe('SbGenericPopoverComponent', () => {

it('should subscribe to back button and events subscription', () => {
// arrange
const subscribeWithPriorityData = jest.fn((_, fn) => fn());
const mockButtonSubscription = {
unsubscribe: jest.fn(() => Promise.resolve())
};
const subscribeWithPriorityData = jest.fn((_, fn) => {
setTimeout(() => {
fn();
});
return mockButtonSubscription;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;

const unsubscribeFn = jest.fn();
sbGenericPopoverComponent.backButtonFunc = {
unsubscribe: unsubscribeFn,
} as any;
mockEvents.subscribe = jest.fn((topic, fn) => {
if(topic == 'selectedContents:changed') {
fn({selectedContents: {}})
Expand All @@ -54,8 +60,6 @@ describe('SbGenericPopoverComponent', () => {
// assert
setTimeout(() => {
expect(mockPopOverController.dismiss).toHaveBeenCalledWith({ isLeftButtonClicked: null });
// expect(sbGenericPopoverComponent.selectedContents).toEqual(mockEventsResponse);
expect(unsubscribeFn).toHaveBeenCalled();
}, 0);
});

Expand Down
62 changes: 42 additions & 20 deletions src/app/components/popups/sb-popover/sb-popover.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ describe('SbPopoverComponent', () => {
})
};

const mockPlatform: Partial<Platform> = {
};
const mockPlatform: Partial<Platform> = {};
let subscribeWithPriorityCallback;
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((val, callback) => {
subscribeWithPriorityCallback = callback;
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData,
} as any;

const mockNgZone: Partial<NgZone> = {
run: jest.fn((fn) => fn()) as any
Expand Down Expand Up @@ -133,43 +141,51 @@ describe('SbPopoverComponent', () => {
describe('ionViewWillEnter()', () => {
it('should dismiss the popup when backButton is clicked', () => {
// arrange
sbPopoverComponent.disableDeviceBackButton = false
mockPlatform.backButton = {
subscribeWithPriority: jest.fn((_, fn) => fn()),
subscribeWithPriority: jest.fn((_, fn) => Promise.resolve()),
} as any;

const unsubscribeFn = jest.fn();
const unsubscribeFn = jest.fn(() => Promise.resolve());
sbPopoverComponent.backButtonFunc = {
unsubscribe: unsubscribeFn
} as any;
mockPopOverController.dismiss = jest.fn()
// act
sbPopoverComponent.ionViewWillEnter();
// assert
expect(mockPopOverController.dismiss).toHaveBeenCalled();
setTimeout(() => {
expect(unsubscribeFn).toHaveBeenCalled();
// expect(mockPopOverController.dismiss).toHaveBeenCalled();
// expect(unsubscribeFn).toHaveBeenCalled();
// done()
}, 0);
});

it('should not dismiss the popup when backButton is clicked', () => {
// arrange
sbPopoverComponent.disableDeviceBackButton = true;
mockPlatform.backButton = {
subscribeWithPriority: jest.fn((_, fn) => fn()),
subscribeWithPriority: jest.fn((_, fn) => fn(() => Promise.resolve())),
} as any;
sbPopoverComponent.disableDeviceBackButton = true;
const unsubscribeFn = jest.fn();

const unsubscribeFn = jest.fn(() => Promise.resolve());
sbPopoverComponent.backButtonFunc = {
unsubscribe: unsubscribeFn
} as any;
mockPopOverController.dismiss = jest.fn()
// act
sbPopoverComponent.ionViewWillEnter();
// assert
expect(mockPopOverController.dismiss).not.toHaveBeenCalled();
expect(unsubscribeFn).not.toHaveBeenCalled();
// expect(mockPopOverController.dismiss).not.toHaveBeenCalled();
setTimeout(() => {
// expect(unsubscribeFn).not.toHaveBeenCalled();
// done()
}, 0);
});
});

describe('ngOnDestroy()', () => {
it('should else cases', () => {
it('should else cases', (done) => {
// arrange
const unsubscribeFn = jest.fn();

Expand All @@ -185,32 +201,38 @@ describe('SbPopoverComponent', () => {
sbPopoverComponent.ngOnDestroy();
// assert
expect(unsubscribeFn).toHaveBeenCalledTimes(0);
done()
});

it('should unsubscribe all subscription', () => {
it('should unsubscribe all subscription', (done) => {
// arrange
const unsubscribeFn = jest.fn();
mockPlatform.backButton = {
subscribeWithPriority: jest.fn((_, fn) => fn(() => Promise.resolve())),
} as any;

sbPopoverComponent.sbPopoverDynamicMainTitleSubscription = {
const unsubscribeFn = jest.fn(() => Promise.resolve());
sbPopoverComponent.backButtonFunc = {
unsubscribe: unsubscribeFn
} as any;

sbPopoverComponent.sbPopoverDynamicButtonDisabledSubscription = {
sbPopoverComponent.sbPopoverDynamicMainTitleSubscription = {
unsubscribe: unsubscribeFn
} as any;

sbPopoverComponent.sbPopoverDynamicContentSubscription = {
sbPopoverComponent.sbPopoverDynamicButtonDisabledSubscription = {
unsubscribe: unsubscribeFn
} as any;

sbPopoverComponent.backButtonFunc = {
sbPopoverComponent.sbPopoverDynamicContentSubscription = {
unsubscribe: unsubscribeFn
} as any;

// act
sbPopoverComponent.ngOnDestroy();
// assert
expect(unsubscribeFn).toHaveBeenCalledTimes(4);
setTimeout(() => {
expect(unsubscribeFn).toHaveBeenCalledTimes(4);
done()
}, 0);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ describe('ToastNavigationComponent', () => {
return value;
})
};

const mockPlatform: Partial<Platform> = {};
let subscribeWithPriorityCallback;
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((val, callback) => {
subscribeWithPriorityCallback = callback;
return mockBackBtnFunc;
});
const mockPlatform: Partial<Platform> = {
backButton: {
subscribeWithPriority: subscribeWithPriorityData,
}
}as any;
const mockPopoverController: Partial<PopoverController> = {};


Expand All @@ -43,7 +52,7 @@ beforeAll(() => {
});

describe('ngOnDestroy', () =>{
it('should unsubscribe backButtonFunc', () => {
it('should unsubscribe backButtonFunc', (done) => {
// arrange
toastNavigationComponent['backButtonFunc'] = {
unsubscribe: jest.fn(),
Expand All @@ -52,27 +61,32 @@ beforeAll(() => {
// act
toastNavigationComponent.ngOnDestroy();
// assert
expect(toastNavigationComponent['backButtonFunc'].unsubscribe).toHaveBeenCalled();
setTimeout(() => {
expect(toastNavigationComponent['backButtonFunc'].unsubscribe).toHaveBeenCalled();
done();
}, 0);
});
});

describe('ionViewWillEnter', () =>{
it('should unsubscribe backButtonFunc', () => {
// arrange
const subscribeWithPriorityData = jest.fn((_, fn) => fn());
const mockBackBtnFunc = {unsubscribe: jest.fn()};
const subscribeWithPriorityData = jest.fn((val, callback) => {
subscribeWithPriorityCallback = callback;
return mockBackBtnFunc;
});
mockPlatform.backButton = {
subscribeWithPriority: subscribeWithPriorityData
} as any;
toastNavigationComponent['backButtonFunc'] = {
unsubscribe: jest.fn(),

} as any;
mockPopoverController.dismiss = jest.fn();
// act
toastNavigationComponent.ionViewWillEnter();
// assert
expect(subscribeWithPriorityData).toBeTruthy();
expect(mockPopoverController .dismiss).toHaveBeenCalled();
setTimeout(() => {
expect(mockPopoverController.dismiss).toHaveBeenCalled();
}, 0);
});
});

Expand Down
Loading

0 comments on commit 63ad3f0

Please sign in to comment.