From 09fa45fc3d8c767572f785b1c72042f7960322c1 Mon Sep 17 00:00:00 2001 From: Eiinu Date: Wed, 20 Mar 2024 13:36:22 +0800 Subject: [PATCH] test: add some unit tests (#2980) --- ...ar.spec.ts.snap => calendar.spec.tsx.snap} | 264 +++++++++--------- .../__VUE/calendar/__tests__/calendar.spec.ts | 153 ---------- .../calendar/__tests__/calendar.spec.tsx | 123 ++++++++ .../__snapshots__/invoice.spec.tsx.snap | 3 + .../__VUE/invoice/__tests__/invoice.spec.tsx | 93 ++++++ .../__snapshots__/signature.spec.tsx.snap | 3 + .../signature/__tests__/signature.spec.tsx | 46 +-- .../__snapshots__/watermark.spec.tsx.snap | 7 + .../watermark/__tests__/watermark.spec.tsx | 51 ++-- 9 files changed, 406 insertions(+), 337 deletions(-) rename src/packages/__VUE/calendar/__tests__/__snapshots__/{calendar.spec.ts.snap => calendar.spec.tsx.snap} (81%) delete mode 100644 src/packages/__VUE/calendar/__tests__/calendar.spec.ts create mode 100644 src/packages/__VUE/calendar/__tests__/calendar.spec.tsx create mode 100644 src/packages/__VUE/invoice/__tests__/__snapshots__/invoice.spec.tsx.snap create mode 100644 src/packages/__VUE/invoice/__tests__/invoice.spec.tsx create mode 100644 src/packages/__VUE/signature/__tests__/__snapshots__/signature.spec.tsx.snap create mode 100644 src/packages/__VUE/watermark/__tests__/__snapshots__/watermark.spec.tsx.snap diff --git a/src/packages/__VUE/calendar/__tests__/__snapshots__/calendar.spec.ts.snap b/src/packages/__VUE/calendar/__tests__/__snapshots__/calendar.spec.tsx.snap similarity index 81% rename from src/packages/__VUE/calendar/__tests__/__snapshots__/calendar.spec.ts.snap rename to src/packages/__VUE/calendar/__tests__/__snapshots__/calendar.spec.tsx.snap index 61b003ecd5..462d7ac750 100644 --- a/src/packages/__VUE/calendar/__tests__/__snapshots__/calendar.spec.ts.snap +++ b/src/packages/__VUE/calendar/__tests__/__snapshots__/calendar.spec.tsx.snap @@ -1,10 +1,10 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`should render slot correctly 1`] = ` +exports[`Calendar: should render slot correctly 1`] = ` " 日期选择 -
最近七天
+
近七天
2022年03月 @@ -19,7 +19,7 @@ exports[`should render slot correctly 1`] = ` " `; -exports[`should render slot correctly 2`] = ` +exports[`Calendar: should render slot correctly 2`] = ` " 2022年02月 @@ -27,7 +27,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -36,7 +36,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -45,7 +45,7 @@ exports[`should render slot correctly 2`] = ` - custom1 + 1 上旬 @@ -54,7 +54,7 @@ exports[`should render slot correctly 2`] = ` - custom2 + 2 上旬 @@ -63,7 +63,7 @@ exports[`should render slot correctly 2`] = ` - custom3 + 3 上旬 @@ -72,7 +72,7 @@ exports[`should render slot correctly 2`] = ` - custom4 + 4 上旬 @@ -81,7 +81,7 @@ exports[`should render slot correctly 2`] = ` - custom5 + 5 上旬 @@ -90,7 +90,7 @@ exports[`should render slot correctly 2`] = ` - custom6 + 6 上旬 @@ -99,7 +99,7 @@ exports[`should render slot correctly 2`] = ` - custom7 + 7 上旬 @@ -108,7 +108,7 @@ exports[`should render slot correctly 2`] = ` - custom8 + 8 上旬 @@ -117,7 +117,7 @@ exports[`should render slot correctly 2`] = ` - custom9 + 9 上旬 @@ -126,7 +126,7 @@ exports[`should render slot correctly 2`] = ` - custom10 + 10 上旬 @@ -135,7 +135,7 @@ exports[`should render slot correctly 2`] = ` - custom11 + 11 中旬 @@ -144,7 +144,7 @@ exports[`should render slot correctly 2`] = ` - custom12 + 12 中旬 @@ -153,7 +153,7 @@ exports[`should render slot correctly 2`] = ` - custom13 + 13 中旬 @@ -162,7 +162,7 @@ exports[`should render slot correctly 2`] = ` - custom14 + 14 中旬 @@ -171,7 +171,7 @@ exports[`should render slot correctly 2`] = ` - custom15 + 15 中旬 @@ -180,7 +180,7 @@ exports[`should render slot correctly 2`] = ` - custom16 + 16 中旬 @@ -189,7 +189,7 @@ exports[`should render slot correctly 2`] = ` - custom17 + 17 中旬 @@ -198,7 +198,7 @@ exports[`should render slot correctly 2`] = ` - custom18 + 18 中旬 @@ -207,7 +207,7 @@ exports[`should render slot correctly 2`] = ` - custom19 + 19 中旬 @@ -216,7 +216,7 @@ exports[`should render slot correctly 2`] = ` - custom20 + 20 中旬 @@ -225,7 +225,7 @@ exports[`should render slot correctly 2`] = ` - custom21 + 21 下旬 @@ -234,7 +234,7 @@ exports[`should render slot correctly 2`] = ` - custom22 + 22 下旬 @@ -243,7 +243,7 @@ exports[`should render slot correctly 2`] = ` - custom23 + 23 下旬 @@ -252,7 +252,7 @@ exports[`should render slot correctly 2`] = ` - custom24 + 24 下旬 @@ -261,7 +261,7 @@ exports[`should render slot correctly 2`] = ` - custom25 + 25 下旬 @@ -270,7 +270,7 @@ exports[`should render slot correctly 2`] = ` - custom26 + 26 下旬 @@ -279,7 +279,7 @@ exports[`should render slot correctly 2`] = ` - custom27 + 27 下旬 @@ -288,7 +288,7 @@ exports[`should render slot correctly 2`] = ` - custom28 + 28 下旬 @@ -304,7 +304,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -313,7 +313,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -322,7 +322,7 @@ exports[`should render slot correctly 2`] = ` - custom1 + 1 上旬 @@ -331,7 +331,7 @@ exports[`should render slot correctly 2`] = ` - custom2 + 2 上旬 @@ -340,7 +340,7 @@ exports[`should render slot correctly 2`] = ` - custom3 + 3 上旬 @@ -349,7 +349,7 @@ exports[`should render slot correctly 2`] = ` - custom4 + 4 上旬 @@ -358,7 +358,7 @@ exports[`should render slot correctly 2`] = ` - custom5 + 5 上旬 @@ -367,7 +367,7 @@ exports[`should render slot correctly 2`] = ` - custom6 + 6 上旬 @@ -376,7 +376,7 @@ exports[`should render slot correctly 2`] = ` - custom7 + 7 上旬 @@ -385,7 +385,7 @@ exports[`should render slot correctly 2`] = ` - custom8 + 8 上旬 @@ -394,7 +394,7 @@ exports[`should render slot correctly 2`] = ` - custom9 + 9 上旬 @@ -403,7 +403,7 @@ exports[`should render slot correctly 2`] = ` - custom10 + 10 上旬 @@ -412,7 +412,7 @@ exports[`should render slot correctly 2`] = ` - custom11 + 11 中旬 @@ -421,7 +421,7 @@ exports[`should render slot correctly 2`] = ` - custom12 + 12 中旬 @@ -430,7 +430,7 @@ exports[`should render slot correctly 2`] = ` - custom13 + 13 中旬 @@ -439,7 +439,7 @@ exports[`should render slot correctly 2`] = ` - custom14 + 14 中旬 @@ -448,7 +448,7 @@ exports[`should render slot correctly 2`] = ` - custom15 + 15 中旬 @@ -457,7 +457,7 @@ exports[`should render slot correctly 2`] = ` - custom16 + 16 中旬 @@ -466,7 +466,7 @@ exports[`should render slot correctly 2`] = ` - custom17 + 17 中旬 @@ -475,7 +475,7 @@ exports[`should render slot correctly 2`] = ` - custom18 + 18 中旬 @@ -484,7 +484,7 @@ exports[`should render slot correctly 2`] = ` - custom19 + 19 中旬 @@ -493,7 +493,7 @@ exports[`should render slot correctly 2`] = ` - custom20 + 20 中旬 @@ -502,7 +502,7 @@ exports[`should render slot correctly 2`] = ` - custom21 + 21 下旬 @@ -511,7 +511,7 @@ exports[`should render slot correctly 2`] = ` - custom22 + 22 下旬 @@ -520,7 +520,7 @@ exports[`should render slot correctly 2`] = ` - custom23 + 23 下旬 @@ -529,7 +529,7 @@ exports[`should render slot correctly 2`] = ` - custom24 + 24 下旬 @@ -538,7 +538,7 @@ exports[`should render slot correctly 2`] = ` - custom25 + 25 下旬 @@ -547,7 +547,7 @@ exports[`should render slot correctly 2`] = ` - custom26 + 26 下旬 @@ -556,7 +556,7 @@ exports[`should render slot correctly 2`] = ` - custom27 + 27 下旬 @@ -565,7 +565,7 @@ exports[`should render slot correctly 2`] = ` - custom28 + 28 下旬 @@ -574,7 +574,7 @@ exports[`should render slot correctly 2`] = ` - custom29 + 29 下旬 @@ -583,7 +583,7 @@ exports[`should render slot correctly 2`] = ` - custom30 + 30 下旬 @@ -592,7 +592,7 @@ exports[`should render slot correctly 2`] = ` - custom31 + 31 下旬 @@ -608,7 +608,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -617,7 +617,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -626,7 +626,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -635,7 +635,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -644,7 +644,7 @@ exports[`should render slot correctly 2`] = ` - customundefined + @@ -653,7 +653,7 @@ exports[`should render slot correctly 2`] = ` - custom1 + 1 上旬 @@ -662,7 +662,7 @@ exports[`should render slot correctly 2`] = ` - custom2 + 2 上旬 @@ -671,7 +671,7 @@ exports[`should render slot correctly 2`] = ` - custom3 + 3 上旬 @@ -680,7 +680,7 @@ exports[`should render slot correctly 2`] = ` - custom4 + 4 上旬 @@ -689,7 +689,7 @@ exports[`should render slot correctly 2`] = ` - custom5 + 5 上旬 @@ -698,7 +698,7 @@ exports[`should render slot correctly 2`] = ` - custom6 + 6 上旬 @@ -707,7 +707,7 @@ exports[`should render slot correctly 2`] = ` - custom7 + 7 上旬 @@ -716,7 +716,7 @@ exports[`should render slot correctly 2`] = ` - custom8 + 8 上旬 @@ -725,7 +725,7 @@ exports[`should render slot correctly 2`] = ` - custom9 + 9 上旬 @@ -734,7 +734,7 @@ exports[`should render slot correctly 2`] = ` - custom10 + 10 上旬 @@ -743,7 +743,7 @@ exports[`should render slot correctly 2`] = ` - custom11 + 11 中旬 @@ -752,7 +752,7 @@ exports[`should render slot correctly 2`] = ` - custom12 + 12 中旬 @@ -761,7 +761,7 @@ exports[`should render slot correctly 2`] = ` - custom13 + 13 中旬 @@ -770,7 +770,7 @@ exports[`should render slot correctly 2`] = ` - custom14 + 14 中旬 @@ -779,7 +779,7 @@ exports[`should render slot correctly 2`] = ` - custom15 + 15 中旬 @@ -788,7 +788,7 @@ exports[`should render slot correctly 2`] = ` - custom16 + 16 中旬 @@ -797,7 +797,7 @@ exports[`should render slot correctly 2`] = ` - custom17 + 17 中旬 @@ -806,7 +806,7 @@ exports[`should render slot correctly 2`] = ` - custom18 + 18 中旬 @@ -815,7 +815,7 @@ exports[`should render slot correctly 2`] = ` - custom19 + 19 中旬 @@ -824,7 +824,7 @@ exports[`should render slot correctly 2`] = ` - custom20 + 20 中旬 @@ -833,7 +833,7 @@ exports[`should render slot correctly 2`] = ` - custom21 + 21 下旬 @@ -842,7 +842,7 @@ exports[`should render slot correctly 2`] = ` - custom22 + 22 下旬 @@ -851,7 +851,7 @@ exports[`should render slot correctly 2`] = ` - custom23 + 23 下旬 @@ -860,7 +860,7 @@ exports[`should render slot correctly 2`] = ` - custom24 + 24 下旬 @@ -869,7 +869,7 @@ exports[`should render slot correctly 2`] = ` - custom25 + 25 下旬 @@ -878,7 +878,7 @@ exports[`should render slot correctly 2`] = ` - custom26 + 26 下旬 @@ -887,7 +887,7 @@ exports[`should render slot correctly 2`] = ` - custom27 + 27 下旬 @@ -896,7 +896,7 @@ exports[`should render slot correctly 2`] = ` - custom28 + 28 下旬 @@ -905,7 +905,7 @@ exports[`should render slot correctly 2`] = ` - custom29 + 29 下旬 @@ -914,7 +914,7 @@ exports[`should render slot correctly 2`] = ` - custom30 + 30 下旬 @@ -930,7 +930,7 @@ exports[`should render slot correctly 2`] = ` - custom1 + 1 上旬 @@ -939,7 +939,7 @@ exports[`should render slot correctly 2`] = ` - custom2 + 2 上旬 @@ -948,7 +948,7 @@ exports[`should render slot correctly 2`] = ` - custom3 + 3 上旬 @@ -957,7 +957,7 @@ exports[`should render slot correctly 2`] = ` - custom4 + 4 上旬 @@ -966,7 +966,7 @@ exports[`should render slot correctly 2`] = ` - custom5 + 5 上旬 @@ -975,7 +975,7 @@ exports[`should render slot correctly 2`] = ` - custom6 + 6 上旬 @@ -984,7 +984,7 @@ exports[`should render slot correctly 2`] = ` - custom7 + 7 上旬 @@ -993,7 +993,7 @@ exports[`should render slot correctly 2`] = ` - custom8 + 8 上旬 @@ -1002,7 +1002,7 @@ exports[`should render slot correctly 2`] = ` - custom9 + 9 上旬 @@ -1011,7 +1011,7 @@ exports[`should render slot correctly 2`] = ` - custom10 + 10 上旬 @@ -1020,7 +1020,7 @@ exports[`should render slot correctly 2`] = ` - custom11 + 11 中旬 @@ -1029,7 +1029,7 @@ exports[`should render slot correctly 2`] = ` - custom12 + 12 中旬 @@ -1038,7 +1038,7 @@ exports[`should render slot correctly 2`] = ` - custom13 + 13 中旬 @@ -1047,7 +1047,7 @@ exports[`should render slot correctly 2`] = ` - custom14 + 14 中旬 @@ -1056,7 +1056,7 @@ exports[`should render slot correctly 2`] = ` - custom15 + 15 中旬 @@ -1065,7 +1065,7 @@ exports[`should render slot correctly 2`] = ` - custom16 + 16 中旬 @@ -1074,7 +1074,7 @@ exports[`should render slot correctly 2`] = ` - custom17 + 17 中旬 @@ -1083,7 +1083,7 @@ exports[`should render slot correctly 2`] = ` - custom18 + 18 中旬 @@ -1092,7 +1092,7 @@ exports[`should render slot correctly 2`] = ` - custom19 + 19 中旬 @@ -1101,7 +1101,7 @@ exports[`should render slot correctly 2`] = ` - custom20 + 20 中旬 @@ -1110,7 +1110,7 @@ exports[`should render slot correctly 2`] = ` - custom21 + 21 下旬 @@ -1119,7 +1119,7 @@ exports[`should render slot correctly 2`] = ` - custom22 + 22 下旬 @@ -1128,7 +1128,7 @@ exports[`should render slot correctly 2`] = ` - custom23 + 23 下旬 @@ -1137,7 +1137,7 @@ exports[`should render slot correctly 2`] = ` - custom24 + 24 下旬 @@ -1146,7 +1146,7 @@ exports[`should render slot correctly 2`] = ` - custom25 + 25 下旬 @@ -1155,7 +1155,7 @@ exports[`should render slot correctly 2`] = ` - custom26 + 26 下旬 @@ -1164,7 +1164,7 @@ exports[`should render slot correctly 2`] = ` - custom27 + 27 下旬 @@ -1173,7 +1173,7 @@ exports[`should render slot correctly 2`] = ` - custom28 + 28 下旬 @@ -1182,7 +1182,7 @@ exports[`should render slot correctly 2`] = ` - custom29 + 29 下旬 @@ -1191,7 +1191,7 @@ exports[`should render slot correctly 2`] = ` - custom30 + 30 下旬 @@ -1200,7 +1200,7 @@ exports[`should render slot correctly 2`] = ` - custom31 + 31 下旬 diff --git a/src/packages/__VUE/calendar/__tests__/calendar.spec.ts b/src/packages/__VUE/calendar/__tests__/calendar.spec.ts deleted file mode 100644 index 0fff1fdb4e..0000000000 --- a/src/packages/__VUE/calendar/__tests__/calendar.spec.ts +++ /dev/null @@ -1,153 +0,0 @@ -import { mount } from '@vue/test-utils'; -import { Calendar } from '@nutui/nutui'; -import { nextTick } from 'vue'; - -test('show-title prop', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - defaultValue: '2022-03-18', - showTitle: true, - startDate: '2022-01-01', - endDate: '2022-12-31' - } - }); - await nextTick(); - expect(wrapper.find('.nut-calendar__header-title').exists()).toBeTruthy(); - await wrapper.setProps({ showTitle: false }); - expect(wrapper.find('.nut-calendar__header-title').exists()).toBeFalsy(); -}); -test('show-sub-title prop', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - defaultValue: '2022-03-18', - showSubTitle: true, - startDate: '2022-01-01', - endDate: '2022-12-31' - } - }); - await nextTick(); - expect(wrapper.find('.nut-calendar__header-subtitle').exists()).toBeTruthy(); - await wrapper.setProps({ showSubTitle: false }); - expect(wrapper.find('.nut-calendar__header-subtitle').exists()).toBeFalsy(); -}); -test('show-today prop', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - defaultValue: '2022-03-18', - showToday: true, - startDate: '2022-01-01', - endDate: '2022-12-31' - } - }); - await nextTick(); - // expect(wrapper.find('.calendar-curr-tip-curr').exists()).toBeTruthy(); - await wrapper.setProps({ showToday: false }); - expect(wrapper.find('.nut-calendar__day-tips--curr').exists()).toBeFalsy(); -}); - -test('first-day-of-week prop', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - firstDayOfWeek: 2, - startDate: '2022-01-01', - endDate: '2022-01-31' - } - }); - await nextTick(); - const weekdays = ['日', '一', '二', '三', '四', '五', '六']; - // 头部周第1个元素 - const weekday = wrapper.findAll('.nut-calendar__weekday')[0].text(); - expect(weekday).toEqual(weekdays[2]); - // 日期面板第7个元素, 判断最后一天的日期是否匹配 - const calendarMonth = wrapper.find('.nut-calendar__month'); - const monthTitle = calendarMonth.find('.nut-calendar__month-title').text(); - const dayText = calendarMonth.findAll('.nut-calendar__day-value')[6].text(); - const date = new Date(monthTitle.replace(/[年月]/g, '/') + dayText); - const index = date.getDay(); - expect(index).toEqual(1); -}); - -test('should render slot correctly', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - defaultValue: '2022-03-18', - startDate: '2022-01-01', - endDate: '2022-12-31' - }, - slots: { - btn: '
最近七天
', - day: (date: any) => { - return `custom${date.date.day}`; - }, - 'bottom-info': (date: any) => - `${date.date ? (date.date.day <= 10 ? '上旬' : date.date.day <= 20 ? '中旬' : '下旬') : ''}` - } - }); - await nextTick(); - expect(wrapper.find('.nut-calendar__header').html()).toMatchSnapshot(); - expect(wrapper.find('.nut-calendar__body').html()).toMatchSnapshot(); -}); - -test('select event when click item', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: true, - defaultValue: '2022-03-18', - showToday: true, - isAutoBackFill: true, - startDate: '2022-01-01', - endDate: '2022-12-31' - } - }); - - await nextTick(); - wrapper.findAll('.nut-calendar__day')[15].trigger('click'); - const arr: any = wrapper.emitted<[Date]>('select')![0][0]; - expect(arr[3]).toEqual('2022-02-14'); -}); -test('choose event when click item', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - defaultValue: '2022-03-18', - showToday: true, - isAutoBackFill: true, - startDate: '2022-01-01', - endDate: '2022-12-31' - } - }); - - await nextTick(); - wrapper.findAll('.nut-calendar__day')[15].trigger('click'); - const arr2: any = wrapper.emitted<[Date]>('choose')![0][0]; - expect(arr2[3]).toEqual('2022-02-14'); -}); -test('Customize the disable date', async () => { - const wrapper = mount(Calendar, { - props: { - poppable: false, - defaultValue: '2022-03-18', - startDate: '2022-01-01', - endDate: '2022-12-31', - disabledDate: (date: string) => { - const disabledDate: { [key: string]: boolean } = { - '2022-01-05': true, - '2022-01-06': true, - '2022-01-10': true, - '2022-01-11': true, - '2022-01-12': true, - '2022-01-13': true, - '2022-01-14': true - }; - return disabledDate[date]; - } - } - }); - await nextTick(); - expect(wrapper.findAll('.nut-calendar__day--disabled').length).toBeGreaterThanOrEqual(7); -}); diff --git a/src/packages/__VUE/calendar/__tests__/calendar.spec.tsx b/src/packages/__VUE/calendar/__tests__/calendar.spec.tsx new file mode 100644 index 0000000000..dd4744406b --- /dev/null +++ b/src/packages/__VUE/calendar/__tests__/calendar.spec.tsx @@ -0,0 +1,123 @@ +import { mount } from '@vue/test-utils'; +import { Calendar } from '@nutui/nutui'; +import { nextTick, ref } from 'vue'; + +test('Calendar: props.showTitle', async () => { + const showTitle = ref(true); + const wrapper = mount(() => { + return ; + }); + expect(wrapper.find('.nut-calendar__header-title').exists()).toBeTruthy(); + showTitle.value = false; + await nextTick(); + expect(wrapper.find('.nut-calendar__header-title').exists()).toBeFalsy(); +}); + +test('Calendar: props.showSubTitle', async () => { + const showSubTitle = ref(true); + const wrapper = mount(() => { + return ; + }); + expect(wrapper.find('.nut-calendar__header-subtitle').exists()).toBeTruthy(); + showSubTitle.value = false; + await nextTick(); + expect(wrapper.find('.nut-calendar__header-subtitle').exists()).toBeFalsy(); +}); + +test('Calendar: props.showToday', async () => { + const showToday = ref(true); + const wrapper = mount(() => { + return ; + }); + await nextTick(); + expect(wrapper.find('.nut-calendar__day-tips--curr').exists()).toBeTruthy(); + showToday.value = false; + await nextTick(); + expect(wrapper.find('.nut-calendar__day-tips--curr').exists()).toBeFalsy(); +}); + +test('Calendar: props.firstDayOfWeek', async () => { + const wrapper = mount(() => { + return ; + }); + await nextTick(); + const weekdays = ['日', '一', '二', '三', '四', '五', '六']; + // 头部周第1个元素 + const weekday = wrapper.findAll('.nut-calendar__weekday')[0].text(); + expect(weekday).toEqual(weekdays[2]); + // 日期面板第7个元素, 判断最后一天的日期是否匹配 + const calendarMonth = wrapper.find('.nut-calendar__month'); + const monthTitle = calendarMonth.find('.nut-calendar__month-title').text(); + const dayText = calendarMonth.findAll('.nut-calendar__day-value')[6].text(); + const date = new Date(monthTitle.replace(/[年月]/g, '/') + dayText); + const index = date.getDay(); + expect(index).toEqual(1); +}); + +test('Calendar: should render slot correctly', async () => { + const wrapper = mount(() => { + return ( + + {{ + btn: () =>
近七天
, + 'bottom-info': (date: any) => + `${date.date ? (date.date.day <= 10 ? '上旬' : date.date.day <= 20 ? '中旬' : '下旬') : ''}` + }} +
+ ); + }); + await nextTick(); + expect(wrapper.find('.nut-calendar__header').html()).toMatchSnapshot(); + expect(wrapper.find('.nut-calendar__body').html()).toMatchSnapshot(); +}); + +test('Calendar: scroll to date & click & choose event', async () => { + const onChoose = vi.fn(); + const calRef = ref(); + const wrapper = mount(() => { + return ( + + ); + }); + + calRef.value.scrollToDate('2024-02-01'); + await nextTick(); + wrapper.findAll('.nut-calendar__days-item .nut-calendar__day')[2].trigger('click'); + expect(onChoose).toBeCalledWith(['2024', '01', '02', '2024-01-02', '星期二']); + wrapper.findAll('.nut-calendar__days-item .nut-calendar__day')[15].trigger('click'); + expect(onChoose).toBeCalledWith(['2024', '01', '15', '2024-01-15', '星期一']); +}); + +test('Calendar: customize the disable date', async () => { + const wrapper = mount(() => { + return ( + { + const disabledDate: { [key: string]: boolean } = { + '2024-01-05': true, + '2024-01-06': true, + '2024-01-10': true, + '2024-01-11': true, + '2024-01-12': true, + '2024-01-13': true, + '2024-01-14': true + }; + return disabledDate[date]; + }} + /> + ); + }); + await nextTick(); + expect(wrapper.findAll('.nut-calendar__day--disabled').length).toBeGreaterThan(7); +}); diff --git a/src/packages/__VUE/invoice/__tests__/__snapshots__/invoice.spec.tsx.snap b/src/packages/__VUE/invoice/__tests__/__snapshots__/invoice.spec.tsx.snap new file mode 100644 index 0000000000..941f5073c5 --- /dev/null +++ b/src/packages/__VUE/invoice/__tests__/__snapshots__/invoice.spec.tsx.snap @@ -0,0 +1,3 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`base 1`] = `"
发票类型企业个人或事业单位发票抬头纳税人识别号注册地址注册电话开户行银行账户
提交审批
"`; diff --git a/src/packages/__VUE/invoice/__tests__/invoice.spec.tsx b/src/packages/__VUE/invoice/__tests__/invoice.spec.tsx new file mode 100644 index 0000000000..8a945f6206 --- /dev/null +++ b/src/packages/__VUE/invoice/__tests__/invoice.spec.tsx @@ -0,0 +1,93 @@ +import { mount } from '@vue/test-utils'; +import { Invoice, type InvoiceDataItem } from '@nutui/nutui'; +import { ref } from 'vue'; + +test('base', () => { + const asyncValidator = (val: string) => { + return new Promise((resolve) => { + setTimeout(() => { + resolve(/^400(-?)[0-9]{7}$|^1\d{10}$|^0[0-9]{2,3}-[0-9]{7,8}$/.test(val)); + }, 1000); + }); + }; + const data = ref([ + { + type: 'radio', + label: '发票类型', + radioLabel: [ + { + label: '企业' + }, + { + label: '个人或事业单位' + } + ], + formItemProp: 'type', + required: true + }, + { + label: '发票抬头', + placeholder: '请输入发票抬头', + formItemProp: 'name', + rules: [{ required: true, message: '请输入发票抬头' }], + required: true + }, + { + label: '纳税人识别号', + placeholder: '请输入纳税人识别号', + formItemProp: 'num', + rules: [{ message: '请输入纳税人识别号' }] + }, + { + label: '注册地址', + placeholder: '请输入注册地址', + formItemProp: 'adress', + rules: [{ required: true, message: '请输入地址' }], + required: true + }, + { + label: '注册电话', + placeholder: '请输入注册电话', + formItemProp: 'tel', + rules: [ + { required: true, message: '请输入联系电话' }, + { validator: asyncValidator, message: '电话格式不正确' } + ], + required: true + }, + { + label: '开户行', + placeholder: '请输入开户行', + formItemProp: 'bank' + }, + { + label: '银行账户', + placeholder: '请输入银行账户', + formItemProp: 'account' + } + ]); + + const formValue = ref({ + type: '企业', + name: '', + num: '', + adress: '', + tel: '', + address: '', + bank: '', + account: '' + }); + + const submit = (valid: boolean, errors: any) => { + if (valid) { + console.log('success', formValue.value); + } else { + console.log('error submit!!', errors); + } + }; + const wrapper = mount(() => { + return ; + }); + const invoice = wrapper.find('.nut-invoice'); + expect(invoice.element.outerHTML).toMatchSnapshot(); +}); diff --git a/src/packages/__VUE/signature/__tests__/__snapshots__/signature.spec.tsx.snap b/src/packages/__VUE/signature/__tests__/__snapshots__/signature.spec.tsx.snap new file mode 100644 index 0000000000..86fc650ba2 --- /dev/null +++ b/src/packages/__VUE/signature/__tests__/__snapshots__/signature.spec.tsx.snap @@ -0,0 +1,3 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`props custom-class 1`] = `"data:image/png;base64,00"`; diff --git a/src/packages/__VUE/signature/__tests__/signature.spec.tsx b/src/packages/__VUE/signature/__tests__/signature.spec.tsx index 7dc3d93674..472fb99f5f 100644 --- a/src/packages/__VUE/signature/__tests__/signature.spec.tsx +++ b/src/packages/__VUE/signature/__tests__/signature.spec.tsx @@ -1,25 +1,25 @@ -// import { mount } from s +import { mount } from '@vue/test-utils'; +import { Signature } from '@nutui/nutui'; +import 'vitest-canvas-mock'; -// TODO -test('需配置 Vitest Canvas Mock', () => { - expect(1).toEqual(1); -}); -// test('props custom-class', async () => { -// const wrapper = mount(() => { -// return ; -// }); -// const signatureWrapper = wrapper.find('.nut-signature'); -// expect(signatureWrapper.classes()).toContain('test'); +test('props custom-class', async () => { + const wrapper = mount(() => { + return ; + }); + const signatureWrapper = wrapper.find('.nut-signature'); + expect(signatureWrapper.classes()).toContain('test'); -// const canvas = createCanvas(400, 400); -// const ctx = canvas.getContext('2d'); -// ctx.lineWidth = 5; -// ctx.strokeStyle = '#333'; -// ctx.beginPath(); -// ctx.lineTo(10, 50); -// ctx.lineTo(80, 120); -// ctx.stroke(); -// const _img = document.createElement('img'); -// _img.src = canvas.toDataURL(); -// expect(canvas.toDataURL()).toMatchSnapshot(); -// }); + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + if (ctx) { + ctx.lineWidth = 5; + ctx.strokeStyle = '#333'; + ctx.beginPath(); + ctx.lineTo(10, 50); + ctx.lineTo(80, 120); + ctx.stroke(); + const _img = document.createElement('img'); + _img.src = canvas.toDataURL(); + expect(canvas.toDataURL()).toMatchSnapshot(); + } +}); diff --git a/src/packages/__VUE/watermark/__tests__/__snapshots__/watermark.spec.tsx.snap b/src/packages/__VUE/watermark/__tests__/__snapshots__/watermark.spec.tsx.snap new file mode 100644 index 0000000000..7ab27ba8e8 --- /dev/null +++ b/src/packages/__VUE/watermark/__tests__/__snapshots__/watermark.spec.tsx.snap @@ -0,0 +1,7 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`should render watermark 1`] = ` +" + +" +`; diff --git a/src/packages/__VUE/watermark/__tests__/watermark.spec.tsx b/src/packages/__VUE/watermark/__tests__/watermark.spec.tsx index 9220043a84..4f0e8f0d47 100644 --- a/src/packages/__VUE/watermark/__tests__/watermark.spec.tsx +++ b/src/packages/__VUE/watermark/__tests__/watermark.spec.tsx @@ -1,32 +1,25 @@ -// import { mount } from '@vue/test-utils'; -// import Watermark from '../index.vue'; -// import { nextTick } from 'vue'; +import { mount } from '@vue/test-utils'; +import { Watermark, Cell } from '@nutui/nutui'; +import { nextTick } from 'vue'; +import 'vitest-canvas-mock'; -// TODO -test('需配置 Vitest Canvas Mock', () => { - expect(1).toEqual(1); +test('render fullPage props ', async () => { + const wrapper = mount(() => { + return ; + }); + await nextTick(); + + expect(wrapper.findAll('.nut-watermark').length).toBe(1); + expect(wrapper.findAll('.nut-watermark-full-page').length).toBe(1); }); -// test('render fullPage props ', async () => { -// const wrapper = mount(() => { -// return ; -// }); -// await nextTick(); -// expect(wrapper.findAll('.nut-watermark').length).toBe(1); -// expect(wrapper.findAll('.nut-watermark-full-page').length).toBe(1); -// }); -// test('should render watermark', () => { -// const wrapper = mount({ -// components: { -// 'nut-cell': Cell, -// 'nut-watermark': Watermark -// }, -// template: ` -// -// -// -// `, -// setup() {} -// }); -// expect(wrapper.html()).toMatchSnapshot(); -// }); +test('should render watermark', () => { + const wrapper = mount(() => { + return ( + + + + ); + }); + expect(wrapper.html()).toMatchSnapshot(); +});