From 8251a7464cea451f77e718112303c9267ec082dd Mon Sep 17 00:00:00 2001 From: VeraZab Date: Tue, 17 Oct 2017 14:14:00 -0400 Subject: [PATCH 1/2] Add tests for table trace type with Plotly.restyle and Plotly.relayout --- test/image/mocks/workspace_starter_state.json | 11 ++ test/jasmine/tests/table_test.js | 135 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 test/image/mocks/workspace_starter_state.json create mode 100644 test/jasmine/tests/table_test.js diff --git a/test/image/mocks/workspace_starter_state.json b/test/image/mocks/workspace_starter_state.json new file mode 100644 index 00000000000..6c9457fabd3 --- /dev/null +++ b/test/image/mocks/workspace_starter_state.json @@ -0,0 +1,11 @@ +{ + "data": [ + { + "mode": "markers", + "type": "scatter" + } + ], + "layout": { + "hovermode": "closest" + } +} diff --git a/test/jasmine/tests/table_test.js b/test/jasmine/tests/table_test.js new file mode 100644 index 00000000000..1c5c50aa052 --- /dev/null +++ b/test/jasmine/tests/table_test.js @@ -0,0 +1,135 @@ +var Plotly = require('@lib'); +var Lib = require('@src/lib'); +var d3 = require('d3'); +var createGraphDiv = require('../assets/create_graph_div'); +var destroyGraphDiv = require('../assets/destroy_graph_div'); + +describe('table plots', function() { + 'use strict'; + var gd; + + beforeEach(function() { + gd = createGraphDiv(); + }); + + afterEach(destroyGraphDiv); + + function initialize(mock, gd) { + return Plotly.plot(gd, mock.data, mock.layout); + } + + function countCells() { + return d3.selectAll('.cell-rect').size(); + } + + it('should be able switch to table trace from another plot type', function(done) { + var mock = Lib.extendDeep({}, require('@mocks/workspace_starter_state.json')); + initialize(mock, gd) + .then(function() { + return Plotly.restyle(gd, {type: 'table'}) + .then(function() { + return Plotly.restyle( + gd, {'header.values[0]': ['First Column']} + ); + }) + .then(function() { + return Plotly.restyle( + gd, {'header.values[1]': ['Second Column']} + ); + }) + .then(function() { + return Plotly.restyle(gd, + { + 'cells.values': [ + ['First Row First Col', 'First Row Second Col'], + ['Second Row First Col', 'Second Row Second Col'] + ] + } + ); + }); + }) + .then(function() { + expect(gd.data[0].type).toBe('table'); + expect(countCells()).toBe(6); + }) + .then(done); + }); + + it('should be able to use Plotly.restyle on an existing table', function(done) { + var mock = Lib.extendDeep({}, require('@mocks/table_plain_birds.json')); + var newColors = ['yellow', 'gray']; + + initialize(mock, gd) + .then(function() { + return Plotly.restyle(gd, {'cells.fill': newColors}) + .then(function() { + Plotly.restyle(gd, {'header.font.size': 20}); + }); + }) + .then(function() { + expect(gd.data[0].cells[0]).toBe('yellow'); + expect(gd.data[0].cells[1]).toBe('gray'); + expect(gd.data[0].header.font.size).toBe(20); + }) + .then(done); + }); + + it('should work with Plotly.restyle for a specific trace index', function(done) { + var mock = Lib.extendDeep({}, require('@mocks/table_latex_multitrace.json')); + var initialFontSizeFirstTrace; + var initialFontSizeSecondTrace; + + initialize(mock, gd) + .then(function() { + initialFontSizeFirstTrace = gd.data[0].header.font.size; + initialFontSizeSecondTrace = gd.data[1].header.font.size; + + expect(initialFontSizeFirstTrace === initialFontSizeSecondTrace).toBe(true); + + return Plotly.restyle(gd, {'header.font.size': initialFontSizeFirstTrace + 1}, 0) + .then(function() { + Plotly.restyle(gd, {'header.font.size': initialFontSizeSecondTrace + 2}, 1); + }); + }) + .then(function() { + expect(gd.data[0].header.font.size).toBe(initialFontSizeFirstTrace + 1); + expect(gd.data[1].header.font.size).toBe(initialFontSizeFirstTrace + 2); + expect(initialFontSizeFirstTrace !== initialFontSizeSecondTrace).toBe(true); + }) + .then(done); + }); + + it('should work with Plotly.relayout', function(done) { + var mock = Lib.extendDeep({}, require('@mocks/table_latex_multitrace.json')); + var newTitle = 'New Title'; + + initialize(mock, gd) + .then(function() { + return Plotly.relayout(gd, {'title': newTitle}); + }) + .then(function() { + expect(gd.layout.title).toBe(newTitle); + }) + .then(done); + }); + + it('should work with Plotly.update', function(done) { + var mock = Lib.extendDeep({}, require('@mocks/table_plain_birds.json')); + var newTitle = 'New Title'; + var initialFontSize; + + initialize(mock, gd) + .then(function() { + initialFontSize = gd.data[0].header.font.size; + return Plotly.update(gd, + {'header.font.size': initialFontSize + 1}, + {'title': newTitle} + ); + }) + .then(function() { + expect(gd.layout.title).toBe(newTitle); + expect(gd.data[0].header.font.size).toBe(initialFontSize + 1); + }) + .then(done); + }); +}); From 1e72abb340b8a2363ba09cc609a95c842ef88477 Mon Sep 17 00:00:00 2001 From: VeraZab Date: Tue, 17 Oct 2017 15:04:28 -0400 Subject: [PATCH 2/2] Remove workspace_starter_state from .json mocks --- test/image/mocks/workspace_starter_state.json | 11 ----------- test/jasmine/tests/table_test.js | 13 ++++++++++++- 2 files changed, 12 insertions(+), 12 deletions(-) delete mode 100644 test/image/mocks/workspace_starter_state.json diff --git a/test/image/mocks/workspace_starter_state.json b/test/image/mocks/workspace_starter_state.json deleted file mode 100644 index 6c9457fabd3..00000000000 --- a/test/image/mocks/workspace_starter_state.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "data": [ - { - "mode": "markers", - "type": "scatter" - } - ], - "layout": { - "hovermode": "closest" - } -} diff --git a/test/jasmine/tests/table_test.js b/test/jasmine/tests/table_test.js index 1c5c50aa052..04264e5eaf8 100644 --- a/test/jasmine/tests/table_test.js +++ b/test/jasmine/tests/table_test.js @@ -23,7 +23,18 @@ describe('table plots', function() { } it('should be able switch to table trace from another plot type', function(done) { - var mock = Lib.extendDeep({}, require('@mocks/workspace_starter_state.json')); + var mock = { + data: [ + { + mode: 'markers', + type: 'scatter' + } + ], + layout: { + hovermode: 'closest' + } + }; + initialize(mock, gd) .then(function() { return Plotly.restyle(gd, {type: 'table'})