/* tslint:disable */
import { getActiveExtensionsEpic } from "./extensions-service";
import { TestScheduler } from "rxjs/testing";
import setupReduxApolloStore from "../__tests__/index";
import * as typeDefs from "../../../../marketplace/server/src/graphql/schemas/schema.graphqls";
import * as rootSchemaDef from "../../../../../servers/backend-server/src/api/root-schema.graphqls";

import { logger } from "@cdm-logger/server";
import "jest";
import { EXTENSION_ACTION_TYPES } from "../constants";

const scheduler = () => new TestScheduler((a, b) => expect(a).toEqual(b));

const reduxInitialState = {
  activatedExtensionIDs: ["Ext1", "Ext2"],
  "@files/edit": {
    openingFiles: [
      {
        name: "Header.jsx",
        resource:
          "file:///usr/src/app/cde-documentation/src/components/Layout/Header.jsx",
        size: 453,
        editorId: 1,
        languageId: 1,
        preOpen: true,
        oversize: false
      },
      {
        name: "Navigation.jsx",
        resource:
          "file:///usr/src/app/cde-documentation/src/components/Layout/Navigation.jsx",
        languageId: 1,
        size: 1160,
        editorId: 1,
        oversize: false
      },
      {
        resource: "/usr/src/app/workbench-stack/Jenkinsfile",
        name: "Jenkinsfile",
        editorId: 1,
        languageId: 0,
        oversize: false
      },
      {
        resource: "/usr/src/app/workbench-stack/package-lock.json",
        name: "package-lock.json",
        languageId: 3,
        editorId: 1,
        oversize: false
      }
    ]
  }
};

const apollMocks = {
  Query: () => ({
    extensions: () => ({
      nodes: [
        {
          id: 1,
          extensionID: "Ext1",
          url: "urlForExt1",
          manifest: {
            raw: "rawForExt1",
            activationEvents: [],
            __typename: "ExtensionManifest"
          },
          viewerCanAdminister: false
        },
        {
          id: 2,
          extensionID: "Ext2",
          url: "urlForExt2",
          manifest: { raw: "rawForExt2", activationEvents: [] },
          viewerCanAdminister: false
        },
        {
          id: 3,
          extensionID: "Ext3",
          url: "urlForExt3",
          manifest: { raw: "rawForExt3", activationEvents: ["*"] },
          viewerCanAdminister: false
        },
        {
          id: 4,
          extensionID: "Ext4",
          url: "urlForExt4",
          manifest: { raw: "rawForExt4", activationEvents: ["onLanguage:jsx"] },
          viewerCanAdminister: false
        },
        {
          id: 5,
          extensionID: "Ext5",
          url: "urlForExt5",
          manifest: {
            raw: "rawForExt5",
            activationEvents: ["onLanguage:js"]
          },
          viewerCanAdminister: false
        }
      ],
      totalCount: 5,
      url: "none",
      error: undefined
    })
  })
};

describe("activeExtensions", () => {
  test("emits an empty set", async done => {
    try {
      const createStore = (initialState, apolloMocks) =>
        setupReduxApolloStore(
          [rootSchemaDef].concat(typeDefs),
          getActiveExtensionsEpic,
          initialState,
          apolloMocks
        );

      const store = createStore(reduxInitialState, apollMocks);
      store.dispatch({
        type: EXTENSION_ACTION_TYPES.FETCH_ACTIVE_EXTENSIONS
      });
      await store.expectActionToBeDispatched(
        EXTENSION_ACTION_TYPES.ACTIVE_EXTENSIONS,
        5
      );
      logger.info("store value [%j]", store.getActions());
      expect(store.getActions()).toMatchInlineSnapshot(`
Array [
  Object {
    "type": "extension/FETCH_ACTIVE_EXTENSIONS",
  },
  Object {
    "payload": Array [
      Object {
        "id": "3",
        "manifest": Object {
          "__typename": "ExtensionManifest",
          "activationEvents": Array [
            "*",
          ],
          "raw": "rawForExt3",
          "title": "Hello World",
        },
      },
      Object {
        "id": "4",
        "manifest": Object {
          "__typename": "ExtensionManifest",
          "activationEvents": Array [
            "onLanguage:jsx",
          ],
          "raw": "rawForExt4",
          "title": "Hello World",
        },
      },
    ],
    "type": "extension/ACTIVE_EXTENSIONS",
  },
]
`);
      done();
    } catch (err) {
      console.error(err);
      done.fail();
    }
  });
});
