/** * Licensed Materials - Property of IBM * IBM Cognos Products: BI * (C) Copyright IBM Corp. 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ import HideNavBarItemLabelsAction from "cawebpack/actions/HideNavBarItemLabelsAction"; const addDOMClass = jest.fn(() => {}); const createAppViewDOM = (navBarLength: number): object => { return { getElementsByClassName: jest.fn(() => { return { length: navBarLength, 0: { classList: { add: addDOMClass } } }; }) }; }; const createMockContext = (appViewDOM: object, homeFlag: boolean): any => { return { glassContext: { appController: { currentAppView: { $el: [appViewDOM], hasHomeFlag: (): boolean => { return homeFlag; } } } } }; }; describe("Hide Navbar Labels Action", () => { let actionHandler: HideNavBarItemLabelsAction; beforeEach(() => { actionHandler = new HideNavBarItemLabelsAction(); const spyFunc = jest.fn(() => createAppViewDOM(1)); Object.defineProperty(document, "querySelector", { value: spyFunc, configurable: true }); }); afterEach(() => { jest.clearAllMocks(); }); it("canExecute returns true if homeFlag false and navBar exists", () => { const appViewDOM = createAppViewDOM(1); const mockContext = createMockContext(appViewDOM, false); const result = actionHandler.canExecute(mockContext); expect(result).toBe(true); }); it("canExecute returns false if homeFlag true and navBar exists", () => { const appViewDOM = createAppViewDOM(1); const mockContext = createMockContext(appViewDOM, true); mockContext.glassContext.appController.currentAppView.hasHomeFlag = (): boolean => { return true; }; const result = actionHandler.canExecute(mockContext); expect(result).toBe(false); }); it("canExecute returns false if homeFlag false and no navBar", () => { const spyFunc = jest.fn(() => createAppViewDOM(0)); Object.defineProperty(document, "querySelector", { value: spyFunc, configurable: true }); const appViewDOM = createAppViewDOM(0); const mockContext = createMockContext(appViewDOM, false); const result = actionHandler.canExecute(mockContext); expect(result).toBe(false); }); it("canExecute returns false if homeFlag true and no navBar", () => { const spyFunc = jest.fn(() => createAppViewDOM(0)); Object.defineProperty(document, "querySelector", { value: spyFunc, configurable: true }); const appViewDOM = createAppViewDOM(0); const mockContext = createMockContext(appViewDOM, true); const result = actionHandler.canExecute(mockContext); expect(result).toBe(false); }); it("it can add class narrow to navbar in doAction", () => { actionHandler.doAction(); expect(addDOMClass).toHaveBeenCalledWith("narrow"); }); });