Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.4 kB
1
Indexable
Never
// test-setup.js
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";

configure({ adapter: new Adapter() });

jest.mock("react-native/Libraries/Utilities/Platform", () => ({
  OS: "macos",
  select: () => null,
}));
jest.mock("../../framework/src/StorageProvider", () => {
  return {
    get: jest.fn().mockResolvedValue("trake"),
  };
});
jest.mock("@kesha-antonov/react-native-action-cable", () => {
  class MockCable {
    constructor() {
      this.channels = {};
    }
    setChannel(channelName, options) {
      const handlers = {};
      this.channels[channelName] = {
        perform: jest.fn(),
        on: jest.fn((event, callback) => {
          handlers[event] = callback;
          callback({
            payload: {
              data: {
                id: "377",
                type: "chat_message",
                attributes: {
                  id: 377,
                  message: "Hello",
                  account_id: 188,
                  chat_id: 121,
                  created_at: "2024-05-15T13:47:21.365Z",
                  updated_at: "2024-05-15T13:47:21.377Z",
                  is_mark_read: true,
                  attachments: null,
                },
              },
              action: "received",
            },
          });
        }),
        handlers,
      };
      return this.channels[channelName];
    }
    channel(channelName) {
      return this.channels[channelName];
    }
  }
  return {
    ActionCable: {
      createConsumer: jest.fn(() => ({
        subscriptions: {
          create: jest.fn(),
        },
      })),
      disconnect: jest.fn(),
    },
    Cable: MockCable,
  };
});
jest.mock("../../framework/src/Utilities", () => ({
  setStorageData: jest.fn().mockImplementation((key, value) => {
    switch (key) {
      case "usertoken":
        return Promise.resolve(value);
      case "refreshtoken":
        return Promise.resolve(value);
      case "userType":
        return Promise.resolve(value);
      default:
        return Promise.resolve(null);
    }
  }),

  getStorageData: jest.fn().mockImplementation((key, parseToJson = false) => {
    switch (key) {
      case "token":
        return Promise.resolve("your-token-value");
      case "userType":
        return Promise.resolve("host");
      default:
        return Promise.resolve(null);
    }
  }),
  removeStorageData: jest
    .fn()
    .mockImplementation((key, parseToJson = false) => {
      switch (key) {
        case "token":
          return Promise.resolve("your-token-value");
        case "host":
          return Promise.resolve("host");
        default:
          return Promise.resolve(null);
      }
    }),
}));

function FormDataMock() {
  this.append = jest.fn();
}

global.FormData = FormDataMock;
jest.mock("react-native-gifted-chat", () => ({
  GiftedChat: {
    append: jest.fn(),
  },
  InputToolbar: jest.fn(),
  Bubble: jest.fn(),
  IMessage: jest.fn(),
  ComposerProps: jest.fn(),
  BubbleProps: jest.fn(),
  InputToolbarProps: jest.fn(),
  SendProps: jest.fn(),
  renderActions: jest.fn(),
  onSend: jest.fn(),
  renderChatFooter: jest.fn(),
  scrollToBottom: jest.fn(),
  renderAvatar: jest.fn(),
  renderComposer: jest.fn(),
  renderDay: jest.fn(),
  renderMessageImage: jest.fn(),
  renderMessageAudio: jest.fn(),
  shouldUpdateMessage: jest.fn(),
  addEventListener: jest.fn(),
  removeEventListener: jest.fn(),
}));
Leave a Comment