Skip to content

Commit ccc31df

Browse files
committed
update tests for new cloud button
1 parent 0a10a22 commit ccc31df

1 file changed

Lines changed: 52 additions & 27 deletions

File tree

test/jasmine/tests/config_test.js

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Plotly = require('../../../lib/index');
22
var Plots = require('../../../src/plots/plots');
33
var Lib = require('../../../src/lib');
4+
var modeBarButtons = require('../../../src/components/modebar/buttons');
45

56
var d3Select = require('../../strict-d3').select;
67
var createGraphDiv = require('../assets/create_graph_div');
@@ -522,69 +523,93 @@ describe('config argument', function() {
522523
describe('plotlyServerURL:', function() {
523524
var gd;
524525
var form;
526+
var openSpy;
525527

526528
beforeEach(function() {
527529
gd = createGraphDiv();
528-
spyOn(HTMLFormElement.prototype, 'submit').and.callFake(function() {
529-
form = this;
530-
});
530+
// sendDataToCloud hands off the chart by opening the provided URL in a
531+
// new tab (window.open(url, '_blank')), so spy on window.open.
532+
var cloudWindow = jasmine.createSpyObj('cloudWindow', ['postMessage']);
533+
openSpy = spyOn(window, 'open').and.returnValue(cloudWindow);
531534
});
532535

533536
afterEach(destroyGraphDiv);
534537

535-
it('should not default to an external plotly cloud', function(done) {
538+
it('should default to an empty string', function(done) {
536539
Plotly.newPlot(gd, [], {})
537540
.then(function() {
538541
expect(gd._context.plotlyServerURL).not.toBe('https://plot.ly');
539542
expect(gd._context.plotlyServerURL).not.toBe('https://chart-studio.plotly.com');
540543
expect(gd._context.plotlyServerURL).toBe('');
541-
542-
Plotly.Plots.sendDataToCloud(gd);
543-
expect(form).toBe(undefined);
544544
})
545545
.then(done, done.fail);
546546
});
547547

548-
it('should be able to connect to Chart Studio Cloud when set to https://chart-studio.plotly.com', function(done) {
548+
it('should open confirmation dialog when set to a correctly-formatted URL', function(done) {
549549
Plotly.newPlot(gd, [], {}, {
550-
plotlyServerURL: 'https://chart-studio.plotly.com'
550+
plotlyServerURL: 'https://example.plotly.com/endpoint'
551551
})
552552
.then(function() {
553-
expect(gd._context.plotlyServerURL).toBe('https://chart-studio.plotly.com');
554-
555-
Plotly.Plots.sendDataToCloud(gd);
556-
expect(form.action).toBe('https://chart-studio.plotly.com/external');
557-
expect(form.method).toBe('post');
553+
expect(gd._context.plotlyServerURL).toBe('https://example.plotly.com/endpoint');
554+
modeBarButtons.sendChartToCloud.click(gd);
555+
var msg = document.querySelector('.plotly-cloud-dialog-message');
556+
expect(msg).not.toBe(null, 'confirmation dialog should be shown');
557+
expect(msg.textContent).toContain('https://example.plotly.com/endpoint');
558558
})
559559
.then(done, done.fail);
560560
});
561561

562-
it('can be set to other base urls', function(done) {
563-
Plotly.newPlot(gd, [], {}, {plotlyServerURL: 'dummy'})
562+
it('should NOT open confirmation dialog when set to an invalid URL', function(done) {
563+
Plotly.newPlot(gd, [], {}, {
564+
plotlyServerURL: 'dummy'
565+
})
564566
.then(function() {
565567
expect(gd._context.plotlyServerURL).toBe('dummy');
568+
modeBarButtons.sendChartToCloud.click(gd);
569+
var msg = document.querySelector('.plotly-cloud-dialog-message');
570+
expect(msg).toBe(null, 'confirmation dialog should not be shown');
571+
})
572+
.then(done, done.fail);
573+
});
566574

567-
Plotly.Plots.sendDataToCloud(gd);
568-
expect(form.action).toContain('/dummy/external');
569-
expect(form.method).toBe('post');
575+
it('should open URL in a new tab after clicking confirm button', function(done) {
576+
Plotly.newPlot(gd, [], {}, {
577+
plotlyServerURL: 'https://example.plotly.com/endpoint'
578+
})
579+
.then(function() {
580+
expect(gd._context.plotlyServerURL).toBe('https://example.plotly.com/endpoint');
581+
modeBarButtons.sendChartToCloud.click(gd);
582+
583+
// Click the confirm button in the dialog
584+
var confirmBtn = document.querySelector('.plotly-cloud-dialog-btn--confirm');
585+
expect(confirmBtn).not.toBe(null, 'confirm button should be shown');
586+
mouseEvent('click', 0, 0, {element: confirmBtn});
587+
588+
// Should open the provided URL's origin in a new tab
589+
expect(openSpy).toHaveBeenCalledWith('https://example.plotly.com/endpoint', '_blank');
570590
})
571591
.then(done, done.fail);
572592
});
573593

574-
it('has lesser priotiy then window env', function(done) {
575-
window.PLOTLYENV = {BASE_URL: 'yo'};
594+
it('has lesser priority than window env', function(done) {
595+
window.PLOTLYENV = {BASE_URL: 'https://yo.plotly.com/endpoint'};
576596

577-
Plotly.newPlot(gd, [], {}, {plotlyServerURL: 'dummy'})
597+
Plotly.newPlot(gd, [], {}, {plotlyServerURL: 'https://example.plotly.com/endpoint2'})
578598
.then(function() {
579-
expect(gd._context.plotlyServerURL).toBe('dummy');
599+
expect(gd._context.plotlyServerURL).toBe('https://example.plotly.com/endpoint2');
600+
601+
// Confirmation dialog message should contain window.PLOTLYENV.BASE_URL,
602+
// which takes priority over plotlyServerURL
603+
modeBarButtons.sendChartToCloud.click(gd);
580604

581-
Plotly.Plots.sendDataToCloud(gd);
582-
expect(form.action).toContain('/yo/external');
583-
expect(form.method).toBe('post');
605+
var msg = document.querySelector('.plotly-cloud-dialog-message');
606+
expect(msg).not.toBe(null, 'confirmation dialog should be shown');
607+
expect(msg.textContent).toContain('https://yo.plotly.com/endpoint');
608+
expect(msg.textContent).not.toContain('https://example.plotly.com/endpoint2');
584609
})
585610
.catch(failTest)
586611
.then(function() {
587-
delete window.PLOTLY_ENV;
612+
delete window.PLOTLYENV;
588613
done();
589614
});
590615
});

0 commit comments

Comments
 (0)