import * as React from 'react';
import {mount} from 'enzyme';
import {TestModule, TestModuleDefinitions} from './TestModule';
import {TestModuleWithNameNonAlphaChars, MODULE_NAME_CONTAINING_A_HYPHEN} from './TestModuleWithNameNonAlphaChars';
import {TestModuleWithNameTooShort, TOO_SHORT_MODULE_NAME} from './TestModuleWithNameTooShort';
import {TestModuleWithNameTooLong, TOO_LONG_MODULE_NAME} from './TestModuleWithNameTooLong';
import {TestAdaptor} from '../../../adaptors/framework/_tests/TestAdaptor';
import {TestProvider} from './TestProvider';
import {TestProviderImpl} from './TestProviderImpl';

describe('BaseModuleTesting', () => {
    const testAdaptor: TestAdaptor = new TestAdaptor();
    const testProvider: TestProvider = new TestProviderImpl();
    const defaultTestModuleProps : TestModuleDefinitions.Props = new TestModuleDefinitions.Props(
        testAdaptor, testProvider);

    it('Should allow modules to be registered if the name is OK.', () : void => {
        const registeredBeforeMount = testAdaptor.isRegistered(TestModuleDefinitions.TEST_MODULE_NAME);
        expect(registeredBeforeMount).toEqual(false);
        const providerPriorToMount = testAdaptor.getProviderByModuleName(TestModuleDefinitions.TEST_MODULE_NAME);
        expect(providerPriorToMount).toBeUndefined();
        mount(<TestModule {...defaultTestModuleProps} />);
        const providerAfterMount = testAdaptor.getProviderByModuleName(TestModuleDefinitions.TEST_MODULE_NAME);
        expect(providerAfterMount).toBeDefined();
        const castTestProvider = providerAfterMount as TestProvider;
        expect(castTestProvider === testProvider).toEqual(true);
    });

    it('Should not allow modules to be registered if the name is too short.', () : void => {
        expect(function() : void {
            mount(<TestModuleWithNameTooShort {...defaultTestModuleProps} />);
        }).toThrow();
        const registered = testAdaptor.isRegistered(TOO_SHORT_MODULE_NAME);
        expect(registered).toEqual(false);
    });

    it('Should not allow modules to be registered if the name is too long.', () : void => {
        expect(function() : void {
            mount(<TestModuleWithNameTooLong {...defaultTestModuleProps} />);
        }).toThrow();
        const registered = testAdaptor.isRegistered(TOO_LONG_MODULE_NAME);
        expect(registered).toEqual(false);
    });

    it('Should not allow modules to be registered if the name contains a hyphen.', () : void => {
        expect(function() : void {
            mount(<TestModuleWithNameNonAlphaChars {...defaultTestModuleProps} />);
        }).toThrow();
        const registered = testAdaptor.isRegistered(MODULE_NAME_CONTAINING_A_HYPHEN);
        expect(registered).toEqual(false);
    });

});
