network.spec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { cancelRunningRequest, sendNetworkRequest } from "../network"
  2. import AxiosStrategy, { cancelRunningAxiosRequest } from "../strategies/AxiosStrategy"
  3. import ExtensionStrategy, {
  4. cancelRunningExtensionRequest,
  5. hasExtensionInstalled,
  6. } from "../strategies/ExtensionStrategy"
  7. jest.mock("../strategies/AxiosStrategy", () => ({
  8. __esModule: true,
  9. default: jest.fn(() => Promise.resolve()),
  10. cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
  11. }))
  12. jest.mock("../strategies/ExtensionStrategy", () => ({
  13. __esModule: true,
  14. default: jest.fn(() => Promise.resolve()),
  15. cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()),
  16. hasExtensionInstalled: jest.fn(),
  17. }))
  18. const extensionAllowedStore = {
  19. state: {
  20. postwoman: {
  21. settings: {
  22. EXTENSIONS_ENABLED: true,
  23. },
  24. },
  25. },
  26. }
  27. const extensionNotAllowedStore = {
  28. state: {
  29. postwoman: {
  30. settings: {
  31. EXTENSIONS_ENABLED: false,
  32. },
  33. },
  34. },
  35. }
  36. const extensionUndefinedStore = {
  37. state: {
  38. postwoman: {
  39. settings: {},
  40. },
  41. },
  42. }
  43. global.$nuxt = {
  44. $loading: {
  45. finish: jest.fn(() => Promise.resolve()),
  46. },
  47. }
  48. beforeEach(() => {
  49. jest.clearAllMocks() // Reset the call count for the mock functions
  50. })
  51. describe("cancelRunningRequest", () => {
  52. test("cancels only extension request if extension allowed in settings and is installed", () => {
  53. hasExtensionInstalled.mockReturnValue(true)
  54. cancelRunningRequest(extensionAllowedStore)
  55. expect(cancelRunningAxiosRequest).not.toHaveBeenCalled()
  56. expect(cancelRunningExtensionRequest).toHaveBeenCalled()
  57. })
  58. test("cancels only extension request if extension setting is undefined and extension is installed", () => {
  59. hasExtensionInstalled.mockReturnValue(true)
  60. cancelRunningRequest(extensionUndefinedStore)
  61. expect(cancelRunningAxiosRequest).not.toHaveBeenCalled()
  62. expect(cancelRunningExtensionRequest).toHaveBeenCalled()
  63. })
  64. test("cancels only axios request if extension not allowed in settings and extension is installed", () => {
  65. hasExtensionInstalled.mockReturnValue(true)
  66. cancelRunningRequest(extensionNotAllowedStore)
  67. expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
  68. expect(cancelRunningAxiosRequest).toHaveBeenCalled()
  69. })
  70. test("cancels only axios request if extension is allowed but not installed", () => {
  71. hasExtensionInstalled.mockReturnValue(false)
  72. cancelRunningRequest(extensionAllowedStore)
  73. expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
  74. expect(cancelRunningAxiosRequest).toHaveBeenCalled()
  75. })
  76. test("cancels only axios request if extension is not allowed and not installed", () => {
  77. hasExtensionInstalled.mockReturnValue(false)
  78. cancelRunningRequest(extensionNotAllowedStore)
  79. expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
  80. expect(cancelRunningAxiosRequest).toHaveBeenCalled()
  81. })
  82. test("cancels only axios request if extension setting is undefined and not installed", () => {
  83. hasExtensionInstalled.mockReturnValue(false)
  84. cancelRunningRequest(extensionUndefinedStore)
  85. expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
  86. expect(cancelRunningAxiosRequest).toHaveBeenCalled()
  87. })
  88. })
  89. describe("sendNetworkRequest", () => {
  90. test("runs only extension request if extension allowed in settings and is installed and clears the progress bar", async () => {
  91. hasExtensionInstalled.mockReturnValue(true)
  92. await sendNetworkRequest({}, extensionAllowedStore)
  93. expect(AxiosStrategy).not.toHaveBeenCalled()
  94. expect(ExtensionStrategy).toHaveBeenCalled()
  95. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  96. })
  97. test("runs only extension request if extension setting is undefined and extension is installed and clears the progress bar", async () => {
  98. hasExtensionInstalled.mockReturnValue(true)
  99. await sendNetworkRequest({}, extensionUndefinedStore)
  100. expect(AxiosStrategy).not.toHaveBeenCalled()
  101. expect(ExtensionStrategy).toHaveBeenCalled()
  102. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  103. })
  104. test("runs only axios request if extension not allowed in settings and extension is installed and clears the progress bar", async () => {
  105. hasExtensionInstalled.mockReturnValue(true)
  106. await sendNetworkRequest({}, extensionNotAllowedStore)
  107. expect(ExtensionStrategy).not.toHaveBeenCalled()
  108. expect(AxiosStrategy).toHaveBeenCalled()
  109. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  110. })
  111. test("runs only axios request if extension is allowed but not installed and clears the progress bar", async () => {
  112. hasExtensionInstalled.mockReturnValue(false)
  113. await sendNetworkRequest({}, extensionAllowedStore)
  114. expect(ExtensionStrategy).not.toHaveBeenCalled()
  115. expect(AxiosStrategy).toHaveBeenCalled()
  116. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  117. })
  118. test("runs only axios request if extension is not allowed and not installed and clears the progress bar", async () => {
  119. hasExtensionInstalled.mockReturnValue(false)
  120. await sendNetworkRequest({}, extensionNotAllowedStore)
  121. expect(ExtensionStrategy).not.toHaveBeenCalled()
  122. expect(AxiosStrategy).toHaveBeenCalled()
  123. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  124. })
  125. test("runs only axios request if extension setting is undefined and not installed and clears the progress bar", async () => {
  126. hasExtensionInstalled.mockReturnValue(false)
  127. await sendNetworkRequest({}, extensionUndefinedStore)
  128. expect(ExtensionStrategy).not.toHaveBeenCalled()
  129. expect(AxiosStrategy).toHaveBeenCalled()
  130. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  131. })
  132. })