|
1 | 1 | var Plotly = require('../../../lib/index'); |
2 | 2 | var Plots = require('../../../src/plots/plots'); |
3 | 3 | var Lib = require('../../../src/lib'); |
| 4 | +var modeBarButtons = require('../../../src/components/modebar/buttons'); |
4 | 5 |
|
5 | 6 | var d3Select = require('../../strict-d3').select; |
6 | 7 | var createGraphDiv = require('../assets/create_graph_div'); |
@@ -522,69 +523,93 @@ describe('config argument', function() { |
522 | 523 | describe('plotlyServerURL:', function() { |
523 | 524 | var gd; |
524 | 525 | var form; |
| 526 | + var openSpy; |
525 | 527 |
|
526 | 528 | beforeEach(function() { |
527 | 529 | 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); |
531 | 534 | }); |
532 | 535 |
|
533 | 536 | afterEach(destroyGraphDiv); |
534 | 537 |
|
535 | | - it('should not default to an external plotly cloud', function(done) { |
| 538 | + it('should default to an empty string', function(done) { |
536 | 539 | Plotly.newPlot(gd, [], {}) |
537 | 540 | .then(function() { |
538 | 541 | expect(gd._context.plotlyServerURL).not.toBe('https://plot.ly'); |
539 | 542 | expect(gd._context.plotlyServerURL).not.toBe('https://chart-studio.plotly.com'); |
540 | 543 | expect(gd._context.plotlyServerURL).toBe(''); |
541 | | - |
542 | | - Plotly.Plots.sendDataToCloud(gd); |
543 | | - expect(form).toBe(undefined); |
544 | 544 | }) |
545 | 545 | .then(done, done.fail); |
546 | 546 | }); |
547 | 547 |
|
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) { |
549 | 549 | Plotly.newPlot(gd, [], {}, { |
550 | | - plotlyServerURL: 'https://chart-studio.plotly.com' |
| 550 | + plotlyServerURL: 'https://example.plotly.com/endpoint' |
551 | 551 | }) |
552 | 552 | .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'); |
558 | 558 | }) |
559 | 559 | .then(done, done.fail); |
560 | 560 | }); |
561 | 561 |
|
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 | + }) |
564 | 566 | .then(function() { |
565 | 567 | 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 | + }); |
566 | 574 |
|
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'); |
570 | 590 | }) |
571 | 591 | .then(done, done.fail); |
572 | 592 | }); |
573 | 593 |
|
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'}; |
576 | 596 |
|
577 | | - Plotly.newPlot(gd, [], {}, {plotlyServerURL: 'dummy'}) |
| 597 | + Plotly.newPlot(gd, [], {}, {plotlyServerURL: 'https://example.plotly.com/endpoint2'}) |
578 | 598 | .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); |
580 | 604 |
|
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'); |
584 | 609 | }) |
585 | 610 | .catch(failTest) |
586 | 611 | .then(function() { |
587 | | - delete window.PLOTLY_ENV; |
| 612 | + delete window.PLOTLYENV; |
588 | 613 | done(); |
589 | 614 | }); |
590 | 615 | }); |
|
0 commit comments