|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | + |
| 3 | +test('Can display a line chart without options', async ({ page }) => { |
| 4 | + await page.goto('/ux-chartjs/without-options'); |
| 5 | + |
| 6 | + // Wait for the canvas element to be visible |
| 7 | + const canvas = page.locator('canvas[data-controller*="chart"]'); |
| 8 | + await expect(canvas).toBeVisible(); |
| 9 | + |
| 10 | + // Wait for the chart instance to be available and access its configuration |
| 11 | + const chartData = await page.waitForFunction(() => { |
| 12 | + return (window as any).__chartInstance !== undefined; |
| 13 | + }).then(() => page.evaluate(() => { |
| 14 | + const chart = (window as any).__chartInstance; |
| 15 | + return { |
| 16 | + type: chart.config.type, |
| 17 | + labels: chart.config.data.labels, |
| 18 | + datasets: chart.config.data.datasets, |
| 19 | + options: chart.config.options, |
| 20 | + }; |
| 21 | + })); |
| 22 | + |
| 23 | + expect(chartData.type).toBe('line'); |
| 24 | + expect(chartData.labels).toEqual(['January', 'February', 'March', 'April', 'May', 'June', 'July']); |
| 25 | + expect(chartData.datasets).toHaveLength(1); |
| 26 | + expect(chartData.datasets[0].label).toBe('My First dataset'); |
| 27 | + expect(chartData.datasets[0].data).toEqual([0, 10, 5, 2, 20, 30, 45]); |
| 28 | + expect(chartData.datasets[0].backgroundColor).toBe('rgb(255, 99, 132)'); |
| 29 | + expect(chartData.datasets[0].borderColor).toBe('rgb(255, 99, 132)'); |
| 30 | +}); |
| 31 | + |
| 32 | +test('Can display a line chart with options', async ({ page }) => { |
| 33 | + await page.goto('/ux-chartjs/with-options'); |
| 34 | + |
| 35 | + // Wait for the canvas element to be visible |
| 36 | + const canvas = page.locator('canvas[data-controller*="chart"]'); |
| 37 | + await expect(canvas).toBeVisible(); |
| 38 | + |
| 39 | + // Wait for the chart instance to be available and access its configuration |
| 40 | + const chartData = await page.waitForFunction(() => { |
| 41 | + return (window as any).__chartInstance !== undefined; |
| 42 | + }).then(() => page.evaluate(() => { |
| 43 | + const chart = (window as any).__chartInstance; |
| 44 | + return { |
| 45 | + type: chart.config.type, |
| 46 | + labels: chart.config.data.labels, |
| 47 | + datasets: chart.config.data.datasets, |
| 48 | + showLines: chart.config.options.showLines, |
| 49 | + }; |
| 50 | + })); |
| 51 | + |
| 52 | + expect(chartData.type).toBe('line'); |
| 53 | + expect(chartData.labels).toEqual(['January', 'February', 'March', 'April', 'May', 'June', 'July']); |
| 54 | + expect(chartData.datasets).toHaveLength(1); |
| 55 | + expect(chartData.datasets[0].data).toEqual([0, 10, 5, 2, 20, 30, 45]); |
| 56 | + expect(chartData.showLines).toBe(false); |
| 57 | +}); |
| 58 | + |
| 59 | +test('Can display a pie chart', async ({ page }) => { |
| 60 | + await page.goto('/ux-chartjs/pie'); |
| 61 | + |
| 62 | + // Wait for the canvas element to be visible |
| 63 | + const canvas = page.locator('canvas[data-controller*="chart"]'); |
| 64 | + await expect(canvas).toBeVisible(); |
| 65 | + |
| 66 | + // Wait for the chart instance to be available and access its configuration |
| 67 | + const chartData = await page.waitForFunction(() => { |
| 68 | + return (window as any).__chartInstance !== undefined; |
| 69 | + }).then(() => page.evaluate(() => { |
| 70 | + const chart = (window as any).__chartInstance; |
| 71 | + return { |
| 72 | + type: chart.config.type, |
| 73 | + labels: chart.config.data.labels, |
| 74 | + datasets: chart.config.data.datasets, |
| 75 | + }; |
| 76 | + })); |
| 77 | + |
| 78 | + expect(chartData.type).toBe('pie'); |
| 79 | + expect(chartData.labels).toEqual(['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']); |
| 80 | + expect(chartData.datasets).toHaveLength(1); |
| 81 | + expect(chartData.datasets[0].label).toBe('My First Dataset'); |
| 82 | + expect(chartData.datasets[0].data).toEqual([12, 19, 3, 5, 2, 3]); |
| 83 | + expect(chartData.datasets[0].backgroundColor).toEqual([ |
| 84 | + 'rgb(255, 99, 132)', |
| 85 | + 'rgb(54, 162, 235)', |
| 86 | + 'rgb(255, 205, 86)', |
| 87 | + 'rgb(75, 192, 192)', |
| 88 | + 'rgb(153, 102, 255)', |
| 89 | + 'rgb(255, 159, 64)', |
| 90 | + ]); |
| 91 | +}); |
| 92 | + |
| 93 | +test('Can display a pie chart with options', async ({ page }) => { |
| 94 | + await page.goto('/ux-chartjs/pie-with-options'); |
| 95 | + |
| 96 | + // Wait for the canvas element to be visible |
| 97 | + const canvas = page.locator('canvas[data-controller*="chart"]'); |
| 98 | + await expect(canvas).toBeVisible(); |
| 99 | + |
| 100 | + // Wait for the chart instance to be available and access its configuration |
| 101 | + const chartData = await page.waitForFunction(() => { |
| 102 | + return (window as any).__chartInstance !== undefined; |
| 103 | + }).then(() => page.evaluate(() => { |
| 104 | + const chart = (window as any).__chartInstance; |
| 105 | + return { |
| 106 | + type: chart.config.type, |
| 107 | + labels: chart.config.data.labels, |
| 108 | + datasets: chart.config.data.datasets, |
| 109 | + responsive: chart.config.options.responsive, |
| 110 | + legendPosition: chart.config.options.plugins?.legend?.position, |
| 111 | + }; |
| 112 | + })); |
| 113 | + |
| 114 | + expect(chartData.type).toBe('pie'); |
| 115 | + expect(chartData.labels).toEqual(['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']); |
| 116 | + expect(chartData.datasets).toHaveLength(1); |
| 117 | + expect(chartData.datasets[0].data).toEqual([12, 19, 3, 5, 2, 3]); |
| 118 | + expect(chartData.responsive).toBe(true); |
| 119 | + expect(chartData.legendPosition).toBe('top'); |
| 120 | +}); |
0 commit comments