|
| 1 | +/* |
| 2 | + * This file is part of the Symfony package. |
| 3 | + * |
| 4 | + * (c) Fabien Potencier <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +import { Application } from '@hotwired/stimulus'; |
| 13 | +import { CheckController } from './chart_check_controller'; |
| 14 | +import { getByTestId, waitFor } from '@testing-library/dom'; |
| 15 | +import { clearDOM, mountDOM } from '@symfony/stimulus-testing'; |
| 16 | +import ChartjsController from '../src/chart_v4_controller'; |
| 17 | + |
| 18 | +const startStimulus = () => { |
| 19 | + const application = Application.start(); |
| 20 | + application.register('check', CheckController); |
| 21 | + application.register('chartjs', ChartjsController); |
| 22 | + |
| 23 | + return application; |
| 24 | +}; |
| 25 | + |
| 26 | +describe('ChartjsController', () => { |
| 27 | + let application; |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + clearDOM(); |
| 31 | + application.stop(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('connect without options', async () => { |
| 35 | + const container = mountDOM(` |
| 36 | + <canvas |
| 37 | + data-testid="canvas" |
| 38 | + data-controller="check chartjs" |
| 39 | + data-chartjs-view-value="{"type":"line","data":{"labels":["January","February","March","April","May","June","July"],"datasets":[{"label":"My First dataset","backgroundColor":"rgb(255, 99, 132)","borderColor":"rgb(255, 99, 132)","data":[0,10,5,2,20,30,45]}]},"options":[]}" |
| 40 | + ></canvas> |
| 41 | + `); |
| 42 | + |
| 43 | + expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected'); |
| 44 | + expect(getByTestId(container, 'canvas')).not.toHaveClass('connected'); |
| 45 | + |
| 46 | + application = startStimulus(); |
| 47 | + |
| 48 | + await waitFor(() => { |
| 49 | + expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected'); |
| 50 | + expect(getByTestId(container, 'canvas')).toHaveClass('connected'); |
| 51 | + }); |
| 52 | + |
| 53 | + const chart = getByTestId(container, 'canvas').chart; |
| 54 | + expect(chart.options.showLines).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('connect with options', async () => { |
| 58 | + const container = mountDOM(` |
| 59 | + <canvas |
| 60 | + data-testid="canvas" |
| 61 | + data-controller="check chartjs" |
| 62 | + data-chartjs-view-value="{"type":"line","data":{"labels":["January","February","March","April","May","June","July"],"datasets":[{"label":"My First dataset","backgroundColor":"rgb(255, 99, 132)","borderColor":"rgb(255, 99, 132)","data":[0,10,5,2,20,30,45]}]},"options":{"showLines":false}}" |
| 63 | + ></canvas> |
| 64 | + `); |
| 65 | + |
| 66 | + expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected'); |
| 67 | + expect(getByTestId(container, 'canvas')).not.toHaveClass('connected'); |
| 68 | + |
| 69 | + application = startStimulus(); |
| 70 | + await waitFor(() => { |
| 71 | + expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected'); |
| 72 | + expect(getByTestId(container, 'canvas')).toHaveClass('connected'); |
| 73 | + }); |
| 74 | + |
| 75 | + const chart = getByTestId(container, 'canvas').chart; |
| 76 | + expect(chart.options.showLines).toBe(false); |
| 77 | + }); |
| 78 | +}); |
0 commit comments