var Plotly = require('../src/plotly');

describe('plot schema', function() {
    'use strict';

    var plotSchema = Plotly.PlotSchema.get(),
        valObjects = plotSchema.defs.valObjects;

    var isValObject = Plotly.PlotSchema.isValObject,
        isPlainObject = Plotly.Lib.isPlainObject;

    function assertPlotSchema(callback) {
        var traces = plotSchema.traces;

        Object.keys(traces).forEach(function(traceName) {
            Plotly.PlotSchema.crawl(traces[traceName].attributes, callback);
        });

        Plotly.PlotSchema.crawl(plotSchema.layout.layoutAttributes, callback);
    }

    it('all attributes should have a valid `valType`', function() {
        var valTypes = Object.keys(valObjects);

        assertPlotSchema(
            function(attr) {
                if(isValObject(attr)) {
                    expect(valTypes.indexOf(attr.valType) !== -1).toBe(true);
                }
            }
        );

    });

    it('all attributes should only have valid `role`', function() {
        var roles = ['info', 'style', 'data'];

        assertPlotSchema(
            function(attr) {
                if(isValObject(attr)) {
                    expect(roles.indexOf(attr.role) !== -1).toBe(true);
                }
            }
        );
    });

    it('all nested objects should have the *object* `role`', function() {
        assertPlotSchema(
            function(attr) {
                if(!isValObject(attr) && isPlainObject(attr)) {
                    expect(attr.role === 'object').toBe(true);
                }
            }
        );
    });

    it('all attributes should have the required options', function() {
        assertPlotSchema(
            function(attr) {
                if(isValObject(attr)) {
                    var keys = Object.keys(attr);

                    valObjects[attr.valType].requiredOpts.forEach(function(opt) {
                        expect(keys.indexOf(opt) !== -1).toBe(true);
                    });
                }
            }
        );
    });

    it('all attributes should only have compatible options', function() {
        assertPlotSchema(
            function(attr) {
                if(isValObject(attr)) {
                    var valObject = valObjects[attr.valType],
                        opts = valObject.requiredOpts
                            .concat(valObject.otherOpts)
                            .concat(['valType', 'description', 'role']);

                    Object.keys(attr).forEach(function(key) {
                        expect(opts.indexOf(key) !== -1).toBe(true);
                    });
                }
            }
        );
    });

    it('all subplot objects should contain _isSubplotObj', function() {
        var IS_SUBPLOT_OBJ = '_isSubplotObj',
            astrs = ['xaxis', 'yaxis', 'scene', 'geo'],
            list = [];

        // check if the subplot objects have '_isSubplotObj'
        astrs.forEach(function(astr) {
            expect(
                Plotly.Lib.nestedProperty(
                    plotSchema.layout.layoutAttributes,
                    astr + '.' + IS_SUBPLOT_OBJ
                ).get()
            ).toBe(true);
        });

        // check that no other object has '_isSubplotObj'
        assertPlotSchema(
            function(attr, attrName) {
                if(attr[IS_SUBPLOT_OBJ] === true) list.push(attrName);
            }
        );
        expect(list).toEqual(astrs);
    });

    it('layout.annotations and layout.shapes should contain _isLinkedToArray', function() {
        var IS_LINKED_TO_ARRAY = '_isLinkedToArray',
            astrs = ['annotations', 'shapes'],
            list = [];

        astrs.forEach(function(astr) {
            expect(
                Plotly.Lib.nestedProperty(
                    plotSchema.layout.layoutAttributes,
                    astr + '.' + IS_LINKED_TO_ARRAY
                ).get()
            ).toBe(true);
        });

        assertPlotSchema(
            function(attr, attrName) {
                if(attr[IS_LINKED_TO_ARRAY] === true) list.push(attrName);
            }
        );
        expect(list).toEqual(astrs);
    });

    it('valObjects descriptions should be strings', function() {
        assertPlotSchema(
            function(attr) {
                var isValid;

                if(isValObject(attr)) {
                    // attribute don't have to have a description (for now)
                    isValid = (typeof attr.description === 'string') ||
                        (attr.description === undefined);

                    expect(isValid).toBe(true);
                }
            }
        );
    });
});

@@ -289,16 +289,104 @@
 bject;%0A%0A
+    var VALTYPES = Object.keys(valObjects),%0A        ROLES = %5B'info', 'style', 'data'%5D;%0A%0A
     func
@@ -415,24 +415,24 @@
 callback) %7B%0A
-
         var 
@@ -766,57 +766,8 @@
 ) %7B%0A
-        var valTypes = Object.keys(valObjects);%0A%0A
     
@@ -888,16 +888,16 @@
 ect(
-valTypes
+VALTYPES
 .ind
@@ -1060,56 +1060,8 @@
 ) %7B%0A
-        var roles = %5B'info', 'style', 'data'%5D;%0A%0A
     
@@ -1182,13 +1182,13 @@
 ect(
-roles
+ROLES
 .ind
@@ -4638,13 +4638,625 @@
 %0A    %7D);
+%0A%0A    it('deprecated attributes should have a %60valType%60 and %60role%60', function() %7B%0A        var DEPRECATED = '_deprecated';%0A%0A        assertPlotSchema(%0A            function(attr) %7B%0A                if(isPlainObject(attr%5BDEPRECATED%5D)) %7B%0A                    Object.keys(attr%5BDEPRECATED%5D).forEach(function(dAttrName) %7B%0A                        var dAttr = attr%5BDEPRECATED%5D%5BdAttrName%5D;%0A%0A                        expect(VALTYPES.indexOf(dAttr.valType) !== -1).toBe(true);%0A                        expect(ROLES.indexOf(dAttr.role) !== -1).toBe(true);%0A                    %7D);%0A                %7D%0A            %7D%0A        );%0A    %7D);
 %0A%7D);%0A

add plotschema test to check deprecated attrs have 'valType' and 'role'