From c8416ad42df5b2438c24edf2cde8f4919003ab48 Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 21 Mar 2023 13:21:05 +0100 Subject: [PATCH] refs #5056 working on tests --- src/pages/Wagon/Type/WagonTypeCreate.vue | 116 +++++++++--------- src/pages/Wagon/WagonCreate.vue | 32 ++--- .../pages/Wagons/WagonTypeCreate.spec.js | 37 ++++++ 3 files changed, 115 insertions(+), 70 deletions(-) create mode 100644 test/vitest/__tests__/pages/Wagons/WagonTypeCreate.spec.js diff --git a/src/pages/Wagon/Type/WagonTypeCreate.vue b/src/pages/Wagon/Type/WagonTypeCreate.vue index c1d40b43f..732a9b556 100644 --- a/src/pages/Wagon/Type/WagonTypeCreate.vue +++ b/src/pages/Wagon/Type/WagonTypeCreate.vue @@ -30,6 +30,66 @@ let wagonConfig; let wagonTypeColors; let currentTrayColorPicked; +async function fetch() { + try { + await axios.get('WagonConfigs').then(async (res) => { + if (res.data) { + wagonConfig = res.data[0]; + } + }); + + await axios.get(`WagonTypeColors`).then(async (res) => { + if (res.data) { + wagonTypeColors = res.data; + if (!entityId.value) + wagon.value.push({ + id: 0, + position: 0, + color: { ...wagonTypeColors[0] }, + action: 'add', + }); + else { + await axios + .get(`WagonTypeTrays`, { + params: { filter: { where: { typeFk: entityId.value } } }, + }) + .then(async (res) => { + if (res.data) { + for (let i = 0; i < res.data.length; i++) { + const tray = res.data[i]; + wagon.value.push({ + id: res.data.length - i - 1, + position: tray.height, + color: { + ...wagonTypeColors.find((color) => { + return color.id === tray.colorFk; + }), + }, + action: tray.height == 0 ? 'add' : 'delete', + }); + } + wagon.value.forEach((value) => { + originalData.trays.push({ ...value }); + }); + } + }); + } + } + }); + + if (entityId.value) { + await axios.get(`WagonTypes/${entityId.value}`).then((res) => { + if (res.data) { + originalData.name = name.value = res.data.name; + originalData.divisible = divisible.value = res.data.divisible; + } + }); + } + } catch (e) { + // + } +} + function addTray() { if ( wagon.value.find((tray) => { @@ -105,62 +165,6 @@ async function onReset() { ]; } -async function fetch() { - await axios.get(`WagonConfigs`).then(async (res) => { - if (res.data) { - wagonConfig = res.data[0]; - } - }); - - await axios.get(`WagonTypeColors`).then(async (res) => { - if (res.data) { - wagonTypeColors = res.data; - if (!entityId.value) - wagon.value.push({ - id: 0, - position: 0, - color: { ...wagonTypeColors[0] }, - action: 'add', - }); - else { - await axios - .get(`WagonTypeTrays`, { - params: { filter: { where: { typeFk: entityId.value } } }, - }) - .then(async (res) => { - if (res.data) { - for (let i = 0; i < res.data.length; i++) { - const tray = res.data[i]; - wagon.value.push({ - id: res.data.length - i - 1, - position: tray.height, - color: { - ...wagonTypeColors.find((color) => { - return color.id === tray.colorFk; - }), - }, - action: tray.height == 0 ? 'add' : 'delete', - }); - } - wagon.value.forEach((value) => { - originalData.trays.push({ ...value }); - }); - } - }); - } - } - }); - - if (entityId.value) { - await axios.get(`WagonTypes/${entityId.value}`).then((res) => { - if (res.data) { - originalData.name = name.value = res.data.name; - originalData.divisible = divisible.value = res.data.divisible; - } - }); - } -} - function doAction(tray) { if (tray.action == 'add') { addTray(); diff --git a/src/pages/Wagon/WagonCreate.vue b/src/pages/Wagon/WagonCreate.vue index e0ff6b022..12ba0b372 100644 --- a/src/pages/Wagon/WagonCreate.vue +++ b/src/pages/Wagon/WagonCreate.vue @@ -50,22 +50,26 @@ async function onReset() { } async function fetch() { - await axios.get(`WagonTypes`).then(async (res) => { - if (res.data) { - filteredWagonTypes.value = wagonTypes = res.data; - } - }); - if (entityId.value) { - await axios.get(`Wagons/${entityId.value}`).then(async (res) => { - const data = res.data; - if (data) { - wagon.value.label = data.label; - wagon.value.plate = data.plate; - wagon.value.volume = data.volume; - wagon.value.typeFk = data.typeFk; - originalData = { ...wagon.value }; + try { + await axios.get('WagonTypes').then(async (res) => { + if (res.data) { + filteredWagonTypes.value = wagonTypes = res.data; } }); + if (entityId.value) { + await axios.get(`Wagons/${entityId.value}`).then(async (res) => { + const data = res.data; + if (data) { + wagon.value.label = data.label; + wagon.value.plate = data.plate; + wagon.value.volume = data.volume; + wagon.value.typeFk = data.typeFk; + originalData = { ...wagon.value }; + } + }); + } + } catch (e) { + // } } diff --git a/test/vitest/__tests__/pages/Wagons/WagonTypeCreate.spec.js b/test/vitest/__tests__/pages/Wagons/WagonTypeCreate.spec.js new file mode 100644 index 000000000..03c3ef07b --- /dev/null +++ b/test/vitest/__tests__/pages/Wagons/WagonTypeCreate.spec.js @@ -0,0 +1,37 @@ +import { createWrapper } from 'app/test/vitest/helper'; +import WagonTypeCreate from 'pages/Wagon/Type/WagonTypeCreate.vue'; +import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; + +describe('WagonTypeCreate', () => { + let vm; + + beforeAll(() => { + vm = createWrapper(WagonTypeCreate, {propsData: { + id: 1, + }}).vm; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('addTray()', () => { + it('should throw message if there are uncompleteTrays', async () => { + vi.spyOn(vm.quasar, 'notify'); + vm.wagon = [{ + id: 1, + position: null, + color: {id: 1, color:'white', rgb:'#000000'} + }]; + + await vm.addTray(); + + expect(vm.quasar.notify).toHaveBeenCalledWith( + expect.objectContaining({ type: 'warning' }) + ); + }); + it('should create a new tray if the limit has not been reached', async () => { + // + }); + }); +});