Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: angular-webcomponents tests #321

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libs/angular-webcomponents/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default {
],
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
moduleNameMapper: {
'^d3-(.*)$': `d3-$1/dist/d3-$1`,
},
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { TestBed } from '@angular/core/testing'
import * as utils from './webcomponent-router-initializer.utils'
import { of } from 'rxjs'
import { AppStateService } from '@onecx/portal-integration-angular'

describe('webcomponent router initializer', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [],
providers: [AppStateService],
}).compileComponents()

jest.clearAllMocks()
})

describe('initializeRouter', () => {
it('should add mfeInfo to routes', async () => {
const routes = [
{
data: {
myProp: 'value',
},
path: 'first',
},
{
data: {
fromRoutesConfig: 'passing value',
},
path: 'second',
},
]
const router = {
config: routes,
resetConfig: jest.fn(),
} as any
const appStateService = TestBed.inject(AppStateService)
jest.spyOn(appStateService.currentMfe$, 'asObservable').mockReturnValue(
of({
mfeInfoProp: 'mockedMfeInfo',
}) as any
)
jest.spyOn(router, 'resetConfig')

expect(router.resetConfig).toHaveBeenCalledTimes(0)

await utils.initializeRouter(router, appStateService)()

expect(router.resetConfig).toHaveBeenCalledTimes(1)
expect(routes).toEqual([
{
data: {
myProp: 'value',
mfeInfo: {
mfeInfoProp: 'mockedMfeInfo',
},
},
path: 'first',
},
{
data: {
fromRoutesConfig: 'passing value',
mfeInfo: {
mfeInfoProp: 'mockedMfeInfo',
},
},
path: 'second',
},
{
path: '**',
children: [],
},
])
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
import { TestBed } from '@angular/core/testing'
import * as router_utils from './webcomponent-router.utils'

describe('webcomponent router utils', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [],
}).compileComponents()

jest.clearAllMocks()
})

describe('startsWith', () => {
it('should consume base href and prefix only', () => {
const emptyMatcher = router_utils.startsWith('')
expect(
emptyMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toEqual({
consumed: [{ path: 'admin' } as any, { path: 'help' } as any],
})
expect(
emptyMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any, { path: 'item' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toEqual({
consumed: [{ path: 'admin' }, { path: 'help' }],
})

const singlePathMatcher = router_utils.startsWith('subPath')
expect(
singlePathMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toBeNull()
expect(
singlePathMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any, { path: 'item' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toBeNull()
expect(
singlePathMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toEqual({
consumed: [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any],
})
expect(
singlePathMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any, { path: 'nested' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toEqual({
consumed: [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any],
})

const complexPathMatcher = router_utils.startsWith('my/nested/subPath')
expect(
complexPathMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toBeNull()
expect(
complexPathMatcher(
[{ path: 'admin' } as any, { path: 'help' } as any, { path: 'item' } as any],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toBeNull()
expect(
complexPathMatcher(
[
{ path: 'admin' } as any,
{ path: 'help' } as any,
{ path: 'my' } as any,
{ path: 'nested' } as any,
{ path: 'subPath' } as any,
],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toEqual({
consumed: [
{ path: 'admin' } as any,
{ path: 'help' } as any,
{ path: 'my' } as any,
{ path: 'nested' } as any,
{ path: 'subPath' } as any,
],
})
expect(
complexPathMatcher(
[
{ path: 'admin' } as any,
{ path: 'help' } as any,
{ path: 'my' } as any,
{ path: 'nested' } as any,
{ path: 'subPath' } as any,
{ path: 'nested2' } as any,
],
{} as any,
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any
)
).toEqual({
consumed: [
{ path: 'admin' } as any,
{ path: 'help' } as any,
{ path: 'my' } as any,
{ path: 'nested' } as any,
{ path: 'subPath' } as any,
],
})
})
})

describe('sliceBaseHref', () => {
it('should remove base href from url', () => {
expect(
router_utils.sliceBaseHref(
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any,
['admin' as any, 'help' as any]
)
).toEqual([])
expect(
router_utils.sliceBaseHref(
{
data: {
mfeInfo: {
baseHref: '/admin/help',
},
},
} as any,
['admin' as any, 'help' as any, 'item' as any]
)
).toEqual(['item' as any])
expect(
router_utils.sliceBaseHref(
{
data: {
mfeInfo: {
baseHref: 'admin/help',
},
},
} as any,
['admin' as any, 'help' as any, 'item' as any]
)
).toEqual(['item' as any])
expect(
router_utils.sliceBaseHref(
{
data: {
mfeInfo: {
baseHref: 'admin/help/',
},
},
} as any,
['admin' as any, 'help' as any, 'item' as any]
)
).toEqual(['item' as any])
})

it('should warn if base href is not defined', () => {
expect(router_utils.sliceBaseHref({} as any, ['admin' as any, 'help' as any])).toEqual([
'admin' as any,
'help' as any,
])
})
})

describe('baseHrefSegmentAmount', () => {
it('should return correct base href segment amount', () => {
expect(router_utils.baseHrefSegmentAmount(['admin' as any, 'help' as any], [])).toEqual(2)
expect(router_utils.baseHrefSegmentAmount([], [])).toEqual(0)
expect(
router_utils.baseHrefSegmentAmount(['admin' as any, 'help' as any, 'item' as any], ['item' as any])
).toEqual(2)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function sliceBaseHref(route: Route, url: UrlSegment[]): UrlSegment[] {
console.warn(
'mfeInfo was not provided for route. initializeRouter function is required to be registered as app initializer.'
)
return url
}

const baseHrefSegmentAmount = mfeBaseHref.split('/').filter((value) => value).length
Expand Down
Loading