HideNavBarItemLabelsAction.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 HideNavBarItemLabelsAction from "cawebpack/actions/HideNavBarItemLabelsAction";
  8. const addDOMClass = jest.fn(() => {});
  9. const createAppViewDOM = (navBarLength: number): object => {
  10. return {
  11. getElementsByClassName: jest.fn(() => {
  12. return {
  13. length: navBarLength,
  14. 0: {
  15. classList: {
  16. add: addDOMClass
  17. }
  18. }
  19. };
  20. })
  21. };
  22. };
  23. const createMockContext = (appViewDOM: object, homeFlag: boolean): any => {
  24. return {
  25. glassContext: {
  26. appController: {
  27. currentAppView: {
  28. $el: [appViewDOM],
  29. hasHomeFlag: (): boolean => {
  30. return homeFlag;
  31. }
  32. }
  33. }
  34. }
  35. };
  36. };
  37. describe("Hide Navbar Labels Action", () => {
  38. let actionHandler: HideNavBarItemLabelsAction;
  39. beforeEach(() => {
  40. actionHandler = new HideNavBarItemLabelsAction();
  41. const spyFunc = jest.fn(() => createAppViewDOM(1));
  42. Object.defineProperty(document, "querySelector", {
  43. value: spyFunc,
  44. configurable: true
  45. });
  46. });
  47. afterEach(() => {
  48. jest.clearAllMocks();
  49. });
  50. it("canExecute returns true if homeFlag false and navBar exists", () => {
  51. const appViewDOM = createAppViewDOM(1);
  52. const mockContext = createMockContext(appViewDOM, false);
  53. const result = actionHandler.canExecute(mockContext);
  54. expect(result).toBe(true);
  55. });
  56. it("canExecute returns false if homeFlag true and navBar exists", () => {
  57. const appViewDOM = createAppViewDOM(1);
  58. const mockContext = createMockContext(appViewDOM, true);
  59. mockContext.glassContext.appController.currentAppView.hasHomeFlag = (): boolean => {
  60. return true;
  61. };
  62. const result = actionHandler.canExecute(mockContext);
  63. expect(result).toBe(false);
  64. });
  65. it("canExecute returns false if homeFlag false and no navBar", () => {
  66. const spyFunc = jest.fn(() => createAppViewDOM(0));
  67. Object.defineProperty(document, "querySelector", {
  68. value: spyFunc,
  69. configurable: true
  70. });
  71. const appViewDOM = createAppViewDOM(0);
  72. const mockContext = createMockContext(appViewDOM, false);
  73. const result = actionHandler.canExecute(mockContext);
  74. expect(result).toBe(false);
  75. });
  76. it("canExecute returns false if homeFlag true and no navBar", () => {
  77. const spyFunc = jest.fn(() => createAppViewDOM(0));
  78. Object.defineProperty(document, "querySelector", {
  79. value: spyFunc,
  80. configurable: true
  81. });
  82. const appViewDOM = createAppViewDOM(0);
  83. const mockContext = createMockContext(appViewDOM, true);
  84. const result = actionHandler.canExecute(mockContext);
  85. expect(result).toBe(false);
  86. });
  87. it("it can add class narrow to navbar in doAction", () => {
  88. actionHandler.doAction();
  89. expect(addDOMClass).toHaveBeenCalledWith("narrow");
  90. });
  91. });