ShowNavBarItemLabelsAction.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Licensed Materials - Property of IBM
  3. * IBM Cognos Products: BI
  4. * (C) Copyright IBM Corp. 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. import ShowNavBarItemLabelsAction from "cawebpack/actions/ShowNavBarItemLabelsAction";
  8. const addDOMClass = jest.fn(() => {});
  9. const removeDOMClass = jest.fn(() => {});
  10. const createAppViewDOM = (navBarLength: number): object => {
  11. return {
  12. getElementsByClassName: jest.fn(() => {
  13. return {
  14. length: navBarLength,
  15. 0: {
  16. classList: {
  17. add: addDOMClass,
  18. remove: removeDOMClass
  19. }
  20. }
  21. };
  22. })
  23. };
  24. };
  25. const createMockContext = (appViewDOM: object): any => {
  26. return {
  27. glassContext: {
  28. appController: {
  29. currentAppView: {
  30. $el: [appViewDOM]
  31. }
  32. }
  33. }
  34. };
  35. };
  36. describe("Hide Navbar Labels Action", () => {
  37. let actionHandler: ShowNavBarItemLabelsAction;
  38. beforeEach(() => {
  39. actionHandler = new ShowNavBarItemLabelsAction();
  40. const spyFunc = jest.fn(() => createAppViewDOM(1));
  41. Object.defineProperty(document, "querySelector", {
  42. value: spyFunc,
  43. configurable: true
  44. });
  45. });
  46. afterEach(() => {
  47. jest.clearAllMocks();
  48. });
  49. it("canExecute returns true if navBar exists", () => {
  50. const appViewDOM = createAppViewDOM(1);
  51. const mockContext = createMockContext(appViewDOM);
  52. const result = actionHandler.canExecute(mockContext);
  53. expect(result).toBe(true);
  54. });
  55. it("canExecute returns false if no navBar", () => {
  56. const spyFunc = jest.fn(() => createAppViewDOM(0));
  57. Object.defineProperty(document, "querySelector", {
  58. value: spyFunc,
  59. configurable: true
  60. });
  61. const appViewDOM = createAppViewDOM(0);
  62. const mockContext = createMockContext(appViewDOM);
  63. const result = actionHandler.canExecute(mockContext);
  64. expect(result).toBe(false);
  65. });
  66. it("it can remove narrow from navbar classList in doAction", () => {
  67. actionHandler.doAction();
  68. expect(removeDOMClass).toHaveBeenCalledWith("narrow");
  69. });
  70. });