/** * 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 ShowNavBarItemLabelsAction from "cawebpack/actions/ShowNavBarItemLabelsAction"; const addDOMClass = jest.fn(() => {}); const removeDOMClass = jest.fn(() => {}); const createAppViewDOM = (navBarLength: number): object => { return { getElementsByClassName: jest.fn(() => { return { length: navBarLength, 0: { classList: { add: addDOMClass, remove: removeDOMClass } } }; }) }; }; const createMockContext = (appViewDOM: object): any => { return { glassContext: { appController: { currentAppView: { $el: [appViewDOM] } } } }; }; describe("Hide Navbar Labels Action", () => { let actionHandler: ShowNavBarItemLabelsAction; beforeEach(() => { actionHandler = new ShowNavBarItemLabelsAction(); const spyFunc = jest.fn(() => createAppViewDOM(1)); Object.defineProperty(document, "querySelector", { value: spyFunc, configurable: true }); }); afterEach(() => { jest.clearAllMocks(); }); it("canExecute returns true if navBar exists", () => { const appViewDOM = createAppViewDOM(1); const mockContext = createMockContext(appViewDOM); const result = actionHandler.canExecute(mockContext); expect(result).toBe(true); }); it("canExecute returns false if no navBar", () => { const spyFunc = jest.fn(() => createAppViewDOM(0)); Object.defineProperty(document, "querySelector", { value: spyFunc, configurable: true }); const appViewDOM = createAppViewDOM(0); const mockContext = createMockContext(appViewDOM); const result = actionHandler.canExecute(mockContext); expect(result).toBe(false); }); it("it can remove narrow from navbar classList in doAction", () => { actionHandler.doAction(); expect(removeDOMClass).toHaveBeenCalledWith("narrow"); }); });