/* eslint-env jasmine */
define ([
    'narrativeViewers',
    'narrativeConfig'
], (
    Viewers,
    Config
) => {
    'use strict';

    // data format taken from
    // https://github.com/kbase/narrative_method_store/blob/master/NarrativeMethodStore.spec#L709
    // and modified to what's needed for running the module and testing
    const categoryData = [
        {},
        {
            'Viewers/a_viewer_app': {},
            'Viewers/error_viewer_app': {
                loading_error: 'Not a real app'
            }
        },
        {},
        {
            'Module.Type1': {
                view_method_ids: ['Viewers/a_viewer_app'],
                landing_page_url_prefix: 'type1',
                name: 'Type 1'
            },
            'Module.Type2': {
                view_method_ids: ['Viewers/missing_viewer_app'],
                landing_page_url_prefix: 'type2',
                name: 'Type 2'
            },
            'Module.Type3': {
                view_method_ids: ['Viewers/error_viewer_app'],
                landing_page_url_prefix: 'type3',
                name: 'Type 3'
            }
        }
    ];
    const methodData = [{
        'Viewers/a_viewer_app': {
            behavior: {},
            fixed_parameters: [],
            info: {
                id: 'Viewers/a_viewer_app',
                module_name: 'Viewers',
                app_type: 'viewer',
                input_types: ['Module.Type1'],
                name: 'View Module Type 1',
            },
            parameters: [],
            widgets: {
                input: 'null',
                output: 'SomeViewerWidget'
            }
        }
    }];

    describe('Test the NarrativeViewers module', () => {
        beforeEach(() => {
            jasmine.Ajax.install();

            jasmine.Ajax.stubRequest(Config.url('narrative_method_store'), /list_categories/)
                .andReturn({
                    status: 200,
                    statusText: 'HTTP/1 200 OK',
                    contentType: 'application/json',
                    responseText: JSON.stringify({
                        version: '1.1',
                        id: '12345',
                        result: categoryData
                    })
                });

            jasmine.Ajax.stubRequest(Config.url('narrative_method_store'), /get_method_spec/)
                .andReturn({
                    status: 200,
                    statusText: 'HTTP/1 200 OK',
                    contentType: 'application/json',
                    responseText: JSON.stringify({
                        version: '1.1',
                        id: '12345',
                        result: methodData
                    })
                });
        });

        afterEach(() => {
            jasmine.Ajax.uninstall();
        });

        it('should load and have expected functions', () => {
            expect(Viewers).toBeDefined();

            ['getViewerInfo', 'createViewer', 'defaultViewer'].forEach(fn => {
                expect(Viewers[fn]).toBeDefined();
            });
        });

        it('should load viewer info as a promise', () => {
            const expectedItems = [
                'viewers', 'typeNames', 'specs', 'methodIds', 'landingPageUrls'
            ];
            return Viewers.getViewerInfo().then((info) => {
                expectedItems.forEach(item => {
                    expect(info[item]).toBeDefined();
                });
            });
        });

        it('should create a viewer, given inputs', () => {
            const dataCell = {
                obj_info: {
                    type: 'Module.Type1-1.0',
                    bare_type: 'Module.Type1'
                }
            };
            return Viewers.createViewer(dataCell).then((view) => {
                expect(view.widget).toBeDefined();
                expect(view.widget.prop('tagName')).toEqual('DIV');
                expect(view.title).toEqual('Type 1');
            });
        });

        it('should still make a default widget when the type doesn\'t exist', () => {
            const dataCell = {
                obj_info: {
                    type: 'NotARealType',
                    bare_type: 'NotARealType'
                }
            };
            return Viewers.createViewer(dataCell).then((view) => {
                expect(view.widget).toBeDefined();
                expect(view.widget.prop('tagName')).toEqual('DIV');
                expect(view.title).toEqual('Unknown Data Type');
            });
        });
    });
});

@@ -134,17 +134,18 @@
 ;%0A%0A    /
-/
+**
  data fo
@@ -164,18 +164,18 @@
 rom%0A    
-//
+ *
  https:/
@@ -262,18 +262,18 @@
 709%0A    
-//
+ *
  and mod
@@ -338,219 +338,728 @@
     
-const categoryData = %5B%0A        %7B%7D,%0A        %7B%0A            'Viewers/a_viewer_app': %7B%7D,%0A            'Viewers/error_viewer_app': %7B%0A                loading_error: 'Not a real app'%0A            %7D%0A        %7D,%0A        %7B%7D,
+ * - returned by NarrativeMethodStore.list_categories%0A     */%0A    const categoryData = %5B%0A        // this would be the list of app categories, ironically not actually needed%0A        %7B%7D,%0A        // list of apps referenced by the types, just mocking to show existence (which is all%0A        // the module needs)%0A        // leaving out one to trigger a different code path%0A        %7B%0A            'Viewers/a_viewer_app': %7B%7D,%0A            'Viewers/error_viewer_app': %7B%0A                loading_error: 'Not a real app'%0A            %7D%0A        %7D,%0A        // list of %22apps%22 (in the NMS lingo), all obsolete and no longer used or needed%0A        %7B%7D,%0A        // set of available types and relevant info used by the NarrativeViewers module
 %0A   
@@ -1641,24 +1641,238 @@
     %7D%0A    %5D;
+%0A%0A    /** %22method%22 data is the NMS lingo for app specs. This just mocks what's needed for%0A     * NarrativeViewers to function for a single mock app.%0A     * - returned by NarrativeMethodStore.get_method_spec%0A     */
 %0A    const m

add some comments to viewers tests