actions.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { downloadConfig } from "corePlugins/configs/spec-actions"
  2. import { loaded } from "corePlugins/configs/actions"
  3. describe("configs plugin - actions", () => {
  4. describe("downloadConfig", () => {
  5. it("should call the system fetch helper with a provided request", () => {
  6. const fetchSpy = jest.fn(async () => {})
  7. const system = {
  8. fn: {
  9. fetch: fetchSpy
  10. }
  11. }
  12. const req = {
  13. url: "http://swagger.io/one",
  14. requestInterceptor: a => a,
  15. responseInterceptor: a => a,
  16. }
  17. downloadConfig(req)(system)
  18. expect(fetchSpy).toHaveBeenCalledWith(req)
  19. })
  20. })
  21. describe("loaded hook", () => {
  22. describe("authorization data restoration", () => {
  23. beforeEach(() => {
  24. localStorage.clear()
  25. })
  26. it("retrieve `authorized` value from `localStorage`", () => {
  27. const system = {
  28. getConfigs: () => ({
  29. persistAuthorization: true
  30. }),
  31. authActions: {
  32. }
  33. }
  34. jest.spyOn(Object.getPrototypeOf(window.localStorage), "getItem")
  35. loaded()(system)
  36. expect(localStorage.getItem).toHaveBeenCalled()
  37. expect(localStorage.getItem).toHaveBeenCalledWith("authorized")
  38. })
  39. it("restore authorization data when a value exists", () => {
  40. const system = {
  41. getConfigs: () => ({
  42. persistAuthorization: true
  43. }),
  44. authActions: {
  45. restoreAuthorization: jest.fn(() => {})
  46. }
  47. }
  48. const mockData = {"api_key": {}}
  49. localStorage.setItem("authorized", JSON.stringify(mockData))
  50. loaded()(system)
  51. expect(system.authActions.restoreAuthorization).toHaveBeenCalled()
  52. expect(system.authActions.restoreAuthorization).toHaveBeenCalledWith({
  53. authorized: mockData
  54. })
  55. })
  56. })
  57. })
  58. })