import { beforeEach, describe, expect, it } from 'vitest'; import { render } from '@testing-library/react'; import { RawParams } from '@uirouter/core'; import * as React from 'react'; import { makeTestRouter } from '../../__tests__/util'; import { UIRouter, UIView } from '../../components'; import { useIsActive } from '../useIsActive'; const state1 = { name: 'state1', url: '/state1' }; const state1Child = { name: 'state1.child', url: '/child' }; const state2 = { name: 'state2', url: '/state2/:param', params: { param: null, param2: null } }; function TestComponent({ state, params, exact }: { state: string; params: RawParams; exact: boolean }) { const isActive = useIsActive(state, params, exact); return
; } describe('useIsActive', () => { let { router, routerGo, mountInRouter } = makeTestRouter([]); beforeEach(() => ({ router, routerGo, mountInRouter } = makeTestRouter([state1, state1Child, state2]))); it('returns true when the route is active', async () => { await routerGo('state2'); const wrapper = mountInRouter(); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); }); it('returns false when the route is not active', async () => { const wrapper = mountInRouter(); await routerGo('state1'); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); }); it('returns true when the route is active and the desired params match', async () => { await routerGo('state2', { param: 'foo' }); const wrapper = mountInRouter(); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); }); it('returns false when the route is active but the desired params do not match', async () => { await routerGo('state2', { param: 'asdf' }); const wrapper = mountInRouter(); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); }); it('returns true when a nested state is active and exact is false', async () => { await routerGo('state1.child'); const wrapper = mountInRouter(); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); }); it('returns false when a nested state is active and exact is true', async () => { const wrapper = mountInRouter(); await routerGo('state1.child'); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); }); it('works with relative states', async () => { const parent = { name: 'parent', component: () => }; const child = { name: 'parent.child', component: () =>
}; router.stateRegistry.register(parent); router.stateRegistry.register(child); await routerGo('parent'); const wrapper = mountInRouter(); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); await routerGo('parent.child'); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); }); it('updates when the desired state changes', async () => { await routerGo('state2'); const wrapper = render( ); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); wrapper.rerender( ); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); }); it('updates when the desired params changes', async () => { await routerGo('state2', { param: 'bar' }); const wrapper = render( ); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); wrapper.rerender( ); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); }); it('updates when the active state changes', async () => { await routerGo('state2'); const wrapper = mountInRouter(); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); await routerGo('state1'); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('yesactive'); await routerGo('state2'); expect(wrapper.getByTestId('div').getAttribute('class')).toBe('notactive'); }); });