network-ExtDisabled.spec.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { cancelRunningRequest, sendNetworkRequest } from "../network"
  2. import AxiosStrategy, {
  3. cancelRunningAxiosRequest,
  4. } from "../strategies/AxiosStrategy"
  5. import ExtensionStrategy, {
  6. cancelRunningExtensionRequest,
  7. hasExtensionInstalled,
  8. } from "../strategies/ExtensionStrategy"
  9. jest.mock("../strategies/AxiosStrategy", () => ({
  10. __esModule: true,
  11. default: jest.fn(() => Promise.resolve()),
  12. cancelRunningAxiosRequest: jest.fn(() => Promise.resolve()),
  13. }))
  14. jest.mock("../strategies/ExtensionStrategy", () => ({
  15. __esModule: true,
  16. default: jest.fn(() => Promise.resolve()),
  17. cancelRunningExtensionRequest: jest.fn(() => Promise.resolve()),
  18. hasExtensionInstalled: jest.fn(),
  19. }))
  20. jest.mock("~/newstore/settings", () => {
  21. return {
  22. settingsStore: {
  23. value: {
  24. EXTENSIONS_ENABLED: false,
  25. },
  26. },
  27. }
  28. })
  29. global.$nuxt = {
  30. $loading: {
  31. finish: jest.fn(() => Promise.resolve()),
  32. },
  33. }
  34. beforeEach(() => {
  35. jest.clearAllMocks() // Reset the call count for the mock functions
  36. })
  37. describe("cancelRunningRequest", () => {
  38. test("cancels only axios request if extension not allowed in settings and extension is installed", () => {
  39. hasExtensionInstalled.mockReturnValue(true)
  40. cancelRunningRequest()
  41. expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
  42. expect(cancelRunningAxiosRequest).toHaveBeenCalled()
  43. })
  44. test("cancels only axios request if extension is not allowed and not installed", () => {
  45. hasExtensionInstalled.mockReturnValue(false)
  46. cancelRunningRequest()
  47. expect(cancelRunningExtensionRequest).not.toHaveBeenCalled()
  48. expect(cancelRunningAxiosRequest).toHaveBeenCalled()
  49. })
  50. })
  51. describe("sendNetworkRequest", () => {
  52. test("runs only axios request if extension not allowed in settings and extension is installed and clears the progress bar", async () => {
  53. hasExtensionInstalled.mockReturnValue(true)
  54. await sendNetworkRequest({})
  55. expect(ExtensionStrategy).not.toHaveBeenCalled()
  56. expect(AxiosStrategy).toHaveBeenCalled()
  57. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  58. })
  59. test("runs only axios request if extension is not allowed and not installed and clears the progress bar", async () => {
  60. hasExtensionInstalled.mockReturnValue(false)
  61. await sendNetworkRequest({})
  62. expect(ExtensionStrategy).not.toHaveBeenCalled()
  63. expect(AxiosStrategy).toHaveBeenCalled()
  64. expect(global.$nuxt.$loading.finish).toHaveBeenCalled()
  65. })
  66. })