/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

import { parse, noopImporter } from '../../../tests/utils';
import isReactCreateClassCall from '../isReactCreateClassCall';

describe('isReactCreateClassCall', () => {
  function parsePath(src) {
    const root = parse(src);
    return root.get('body', root.node.body.length - 1, 'expression');
  }

  describe('built in React.createClass', () => {
    it('accepts createClass called on React', () => {
      const def = parsePath(`
        var React = require("React");
        React.createClass({
          render() {}
        });
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('accepts createClass called on aliased React', () => {
      const def = parsePath(`
        var other = require("React");
        other.createClass({
          render() {}
        });
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('ignores other React calls', () => {
      const def = parsePath(`
        var React = require("React");
        React.isValidElement({});
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(false);
    });

    it('ignores non React calls to createClass', () => {
      const def = parsePath(`
        var React = require("bob");
        React.createClass({
          render() {}
        });
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(false);
    });

    it('accepts createClass called on destructed value', () => {
      const def = parsePath(`
        var { createClass } = require("react");
        createClass({});
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('accepts createClass called on destructed aliased value', () => {
      const def = parsePath(`
        var { createClass: foo } = require("react");
        foo({});
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('accepts createClass called on imported value', () => {
      const def = parsePath(`
        import { createClass } from "react";
        createClass({});
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('accepts createClass called on imported aliased value', () => {
      const def = parsePath(`
        import { createClass as foo } from "react";
        foo({});
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });
  });

  describe('modular in create-react-class', () => {
    it('accepts create-react-class', () => {
      const def = parsePath(`
        var createReactClass = require("create-react-class");
        createReactClass({
          render() {}
        });
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('accepts create-react-class calls on another name', () => {
      const def = parsePath(`
        var makeClass = require("create-react-class");
        makeClass({
          render() {}
        });
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(true);
    });

    it('ignores non create-react-class calls to createReactClass', () => {
      const def = parsePath(`
        var createReactClass = require("bob");
        createReactClass({
          render() {}
        });
      `);
      expect(isReactCreateClassCall(def, noopImporter)).toBe(false);
    });
  });
});

@@ -198,29 +198,67 @@
 rt %7B
+%0A 
  parse,
- noopImporter 
+%0A  statement,%0A  noopImporter,%0A  makeMockImporter,%0A
 %7D fr
@@ -523,13 +523,314 @@
 ');%0A
-
   %7D%0A%0A
+  const mockImporter = makeMockImporter(%7B%0A    foo: statement(%60%0A      export default React.createClass;%0A      import React from 'react';%0A    %60).get('declaration'),%0A%0A    bar: statement(%60%0A      export default makeClass;%0A      import makeClass from %22create-react-class%22;%0A    %60).get('declaration'),%0A  %7D);%0A%0A
   de
@@ -2947,24 +2947,262 @@
 ue);%0A    %7D);
+%0A%0A    it('resolves createClass imported from intermediate module', () =%3E %7B%0A      const def = parsePath(%60%0A        import foo from %22foo%22;%0A        foo(%7B%7D);%0A      %60);%0A      expect(isReactCreateClassCall(def, mockImporter)).toBe(true);%0A    %7D);
 %0A  %7D);%0A%0A  de
@@ -4122,23 +4122,268 @@
 se);%0A    %7D);
+%0A%0A    it('resolves create-react-class imported from intermediate module', () =%3E %7B%0A      const def = parsePath(%60%0A        import bar from %22bar%22;%0A        bar(%7B%7D);%0A      %60);%0A      expect(isReactCreateClassCall(def, mockImporter)).toBe(true);%0A    %7D);
 %0A  %7D);%0A%7D);%0A

Add importer tests for isReactCreateClassCall