From 5bd58fdf0413fe667d593e62ee51e0bc6a7aab3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bahad=C4=B1r=20Sofuo=C4=9Flu?= Date: Wed, 4 Jan 2023 18:24:25 +0300 Subject: [PATCH] BreadcrumbItem unit test updated --- components/breadcrumb/BreadcrumbItem.spec.js | 90 ++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 components/breadcrumb/BreadcrumbItem.spec.js diff --git a/components/breadcrumb/BreadcrumbItem.spec.js b/components/breadcrumb/BreadcrumbItem.spec.js new file mode 100644 index 000000000..44d0a587f --- /dev/null +++ b/components/breadcrumb/BreadcrumbItem.spec.js @@ -0,0 +1,90 @@ +import { RouterLinkStub, shallowMount } from '@vue/test-utils'; +import { afterEach, beforeEach, expect } from 'vitest'; +import BreadcrumbItem from './BreadcrumbItem.vue'; + +let wrapper = null; + +beforeEach(() => { + wrapper = shallowMount(BreadcrumbItem, { + global: { + mocks: { + $router: { + currentRoute: { + path: vi.fn() + }, + navigate: () => true + } + }, + stubs: { + 'router-link': RouterLinkStub + } + }, + props: { + item: { label: 'Computer', visible: () => true }, + template: null + } + }); + + afterEach(() => { + vi.clearAllMocks(); + }); +}); +describe('BreadcrumbItem', () => { + it('When component is mount, text should be exists', () => { + expect(wrapper.find('.p-menuitem-text').exists()).toBe(true); + }); + + it('When tag is triggered, onClick method should be called', async () => { + const onClickSpy = vi.spyOn(wrapper.vm, 'onClick'); + const tag = wrapper.find('a'); + + tag.trigger('click'); + expect(tag.exists()).toBe(true); + expect(onClickSpy).toHaveBeenCalled(); + }); + + it('When onClick method called and item has a command callback, callback should be triggered', async () => { + await wrapper.setProps({ item: { label: 'Computer', visible: () => false, command: vi.fn() } }); + + const spy = vi.spyOn(wrapper.vm.item, 'command'); + + wrapper.vm.onClick(); + expect(spy).toHaveBeenCalled(); + }); + + it('When linkClass method called and isActive-isExactActive, tag classes should be effected', async () => { + await wrapper.setProps({ exact: true }); + + expect(wrapper.vm.linkClass({ isActive: true, isExactActive: true })).toEqual([ + 'p-menuitem-link', + { + 'router-link-active': true, + 'router-link-active-exact': true + } + ]); + }); + + it('When item prop has a visible, visible method should be return falsy', async () => { + await wrapper.setProps({ item: { label: 'Computer', visible: false, command: vi.fn() } }); + + expect(wrapper.vm.visible()).toBeFalsy(); + }); + + it('When item prop has a disabled function, disabled method should be return truthy', async () => { + await wrapper.setProps({ item: { disabled: () => true } }); + + expect(wrapper.vm.disabled()).toBeTruthy(); + }); + + it('When item prop has a label function, disabled method should be return truthy', async () => { + await wrapper.setProps({ item: { label: () => true } }); + + expect(wrapper.vm.label()).toBeTruthy(); + }); + + it('When item prop has a icon, icon element class should contain item prop icon', async () => { + await wrapper.setProps({ item: { icon: 'pi-discord' } }); + + expect(wrapper.find('a > span').classes()).toContain('pi-discord'); + }); +});