mirror of https://github.com/abpframework/abp.git
20 changed files with 735 additions and 1517 deletions
@ -1,281 +1,201 @@ |
|||||
import { HttpClient } from '@angular/common/http'; |
import { HttpClient } from '@angular/common/http'; |
||||
import { Component, inject as inject_1 } from '@angular/core'; |
import { Component, NgModule, inject as inject_1 } from '@angular/core'; |
||||
import { ActivatedRoute, RouterModule, RouterOutlet } from '@angular/router'; |
import { ActivatedRoute, RouterModule } from '@angular/router'; |
||||
import { of, BehaviorSubject } from 'rxjs'; |
import { createRoutingFactory, SpectatorRouting } from '@ngneat/spectator/jest'; |
||||
import { createRoutingFactory, SpectatorRouting } from '@ngneat/spectator/jest'; |
import { DynamicLayoutComponent, RouterOutletComponent } from '../components'; |
||||
import { DynamicLayoutComponent, RouterOutletComponent } from '../components'; |
import { eLayoutType } from '../enums/common'; |
||||
import { eLayoutType } from '../enums/common'; |
import { ABP } from '../models'; |
||||
import { ABP } from '../models'; |
import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; |
||||
import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; |
import { ReplaceableComponentsService, RoutesService } from '../services'; |
||||
import { ReplaceableComponentsService, RoutesService, RouterEvents, EnvironmentService, LocalizationService } from '../services'; |
import { mockRoutesService } from './routes.service.spec'; |
||||
import { DYNAMIC_LAYOUTS_TOKEN } from '../tokens/dynamic-layout.token'; |
|
||||
|
@Component({ |
||||
const mockRoutesService = () => ({ |
selector: 'abp-layout-application', |
||||
add: jest.fn(), |
template: '<router-outlet></router-outlet>', |
||||
find: jest.fn((predicate) => { |
}) |
||||
|
class DummyApplicationLayoutComponent {} |
||||
if (predicate && typeof predicate === 'function') { |
|
||||
if (predicate({ path: '/parentWithLayout/childWithoutLayout' })) { |
@Component({ |
||||
return { layout: eLayoutType.application }; |
selector: 'abp-layout-account', |
||||
} |
template: '<router-outlet></router-outlet>', |
||||
if (predicate({ path: '/parentWithLayout/childWithLayout' })) { |
}) |
||||
return { layout: eLayoutType.account }; |
class DummyAccountLayoutComponent {} |
||||
} |
|
||||
if (predicate({ path: '/withData' })) { |
@Component({ |
||||
return { layout: eLayoutType.empty }; |
selector: 'abp-layout-empty', |
||||
} |
template: '<router-outlet></router-outlet>', |
||||
if (predicate({ path: '/withoutLayout' })) { |
}) |
||||
return { layout: null }; |
class DummyEmptyLayoutComponent {} |
||||
} |
|
||||
} |
const LAYOUTS = [ |
||||
return null; |
DummyApplicationLayoutComponent, |
||||
}), |
DummyAccountLayoutComponent, |
||||
search: jest.fn((query) => { |
DummyEmptyLayoutComponent, |
||||
|
]; |
||||
if (query && query.invisible) { |
|
||||
return { layout: eLayoutType.account }; |
@NgModule({ |
||||
} |
imports: [RouterModule], |
||||
return null; |
declarations: [...LAYOUTS], |
||||
}), |
}) |
||||
flat$: of([]), |
class DummyLayoutModule {} |
||||
tree$: of([]), |
|
||||
visible$: of([]), |
@Component({ |
||||
}); |
selector: 'abp-dummy', |
||||
|
template: '{{route.snapshot.data?.name}} works!', |
||||
@Component({ |
}) |
||||
selector: 'abp-layout-application', |
class DummyComponent {
route = inject_1(ActivatedRoute); |
||||
template: '<router-outlet></router-outlet>', |
|
||||
standalone: true, |
} |
||||
imports: [RouterOutlet], |
|
||||
}) |
const routes: ABP.Route[] = [ |
||||
class DummyApplicationLayoutComponent {} |
{ |
||||
|
path: '', |
||||
@Component({ |
name: 'Root', |
||||
selector: 'abp-layout-account', |
}, |
||||
template: '<router-outlet></router-outlet>', |
{ |
||||
standalone: true, |
path: '/parentWithLayout', |
||||
imports: [RouterOutlet], |
name: 'ParentWithLayout', |
||||
}) |
parentName: 'Root', |
||||
class DummyAccountLayoutComponent {} |
layout: eLayoutType.application, |
||||
|
}, |
||||
@Component({ |
{ |
||||
selector: 'abp-layout-empty', |
path: '/parentWithLayout/childWithoutLayout', |
||||
template: '<router-outlet></router-outlet>', |
name: 'ChildWithoutLayout', |
||||
standalone: true, |
parentName: 'ParentWithLayout', |
||||
imports: [RouterOutlet], |
}, |
||||
}) |
{ |
||||
class DummyEmptyLayoutComponent {} |
path: '/parentWithLayout/childWithLayout', |
||||
|
name: 'ChildWithLayout', |
||||
const LAYOUTS = [ |
parentName: 'ParentWithLayout', |
||||
DummyApplicationLayoutComponent, |
layout: eLayoutType.account, |
||||
DummyAccountLayoutComponent, |
}, |
||||
DummyEmptyLayoutComponent, |
{ |
||||
]; |
path: '/withData', |
||||
|
name: 'WithData', |
||||
@Component({ |
layout: eLayoutType.application, |
||||
selector: 'abp-dummy', |
}, |
||||
template: '{{route.snapshot.data?.name}} works!', |
]; |
||||
standalone: true, |
|
||||
}) |
describe('DynamicLayoutComponent', () => { |
||||
class DummyComponent { |
const createComponent = createRoutingFactory({ |
||||
route = inject_1(ActivatedRoute); |
component: RouterOutletComponent, |
||||
} |
stubsEnabled: false, |
||||
|
declarations: [DummyComponent, DynamicLayoutComponent], |
||||
const routes: ABP.Route[] = [ |
mocks: [AbpApplicationConfigurationService, HttpClient], |
||||
{ |
providers: [ |
||||
path: '', |
{ |
||||
name: 'Root', |
provide: RoutesService, |
||||
}, |
useFactory: () => mockRoutesService(), |
||||
{ |
}, |
||||
path: '/parentWithLayout', |
ReplaceableComponentsService, |
||||
name: 'ParentWithLayout', |
], |
||||
parentName: 'Root', |
imports: [RouterModule, DummyLayoutModule], |
||||
layout: eLayoutType.application, |
routes: [ |
||||
}, |
{ path: '', component: RouterOutletComponent }, |
||||
{ |
{ |
||||
path: '/parentWithLayout/childWithoutLayout', |
path: 'parentWithLayout', |
||||
name: 'ChildWithoutLayout', |
component: DynamicLayoutComponent, |
||||
parentName: 'ParentWithLayout', |
children: [ |
||||
}, |
{ |
||||
{ |
path: 'childWithoutLayout', |
||||
path: '/parentWithLayout/childWithLayout', |
component: DummyComponent, |
||||
name: 'ChildWithLayout', |
data: { name: 'childWithoutLayout' }, |
||||
parentName: 'ParentWithLayout', |
}, |
||||
layout: eLayoutType.account, |
{ |
||||
}, |
path: 'childWithLayout', |
||||
{ |
component: DummyComponent, |
||||
path: '/withData', |
data: { name: 'childWithLayout' }, |
||||
name: 'WithData', |
}, |
||||
layout: eLayoutType.application, |
], |
||||
}, |
}, |
||||
]; |
{ |
||||
|
path: 'withData', |
||||
describe('DynamicLayoutComponent', () => { |
component: DynamicLayoutComponent, |
||||
const createComponent = createRoutingFactory({ |
children: [ |
||||
component: DynamicLayoutComponent, |
{ |
||||
stubsEnabled: false, |
path: '', |
||||
declarations: [], |
component: DummyComponent, |
||||
mocks: [AbpApplicationConfigurationService, HttpClient], |
data: { name: 'withData' }, |
||||
providers: [ |
}, |
||||
{ |
], |
||||
provide: RoutesService, |
data: { layout: eLayoutType.empty }, |
||||
useValue: mockRoutesService(), |
}, |
||||
}, |
{ |
||||
{ |
path: 'withoutLayout', |
||||
provide: RouterEvents, |
component: DynamicLayoutComponent, |
||||
useValue: { |
children: [ |
||||
getNavigationEvents: jest.fn().mockReturnValue(of({})), |
{ |
||||
}, |
path: '', |
||||
}, |
component: DummyComponent, |
||||
{ |
data: { name: 'withoutLayout' }, |
||||
provide: EnvironmentService, |
}, |
||||
useValue: { |
], |
||||
getEnvironment: jest.fn().mockReturnValue({ |
data: { layout: null }, |
||||
oAuthConfig: { responseType: 'code' }, |
}, |
||||
}), |
], |
||||
}, |
}); |
||||
}, |
|
||||
{ |
let spectator: SpectatorRouting<RouterOutletComponent>; |
||||
provide: LocalizationService, |
let replaceableComponents: ReplaceableComponentsService; |
||||
useValue: { |
|
||||
languageChange$: new BehaviorSubject('en'), |
beforeEach(async () => { |
||||
}, |
spectator = createComponent(); |
||||
}, |
replaceableComponents = spectator.inject(ReplaceableComponentsService); |
||||
{ |
const routesService = spectator.inject(RoutesService); |
||||
provide: DYNAMIC_LAYOUTS_TOKEN, |
routesService.add(routes); |
||||
useValue: new Map([ |
|
||||
[eLayoutType.application, 'Theme.ApplicationLayoutComponent'], |
replaceableComponents.add({ |
||||
[eLayoutType.account, 'Theme.AccountLayoutComponent'], |
key: 'Theme.ApplicationLayoutComponent', |
||||
[eLayoutType.empty, 'Theme.EmptyLayoutComponent'], |
component: DummyApplicationLayoutComponent, |
||||
]), |
}); |
||||
}, |
replaceableComponents.add({ |
||||
{ |
key: 'Theme.AccountLayoutComponent', |
||||
provide: ReplaceableComponentsService, |
component: DummyAccountLayoutComponent, |
||||
useValue: { |
}); |
||||
add: jest.fn(), |
replaceableComponents.add({ |
||||
get: jest.fn((key) => { |
key: 'Theme.EmptyLayoutComponent', |
||||
if (key === 'Theme.ApplicationLayoutComponent') { |
component: DummyEmptyLayoutComponent, |
||||
return { component: DummyApplicationLayoutComponent }; |
}); |
||||
} |
}); |
||||
if (key === 'Theme.AccountLayoutComponent') { |
|
||||
return { component: DummyAccountLayoutComponent }; |
it('should handle application layout from parent abp route and display it', async () => { |
||||
} |
spectator.router.navigateByUrl('/parentWithLayout/childWithoutLayout'); |
||||
if (key === 'Theme.EmptyLayoutComponent') { |
await spectator.fixture.whenStable(); |
||||
return { component: DummyEmptyLayoutComponent }; |
spectator.detectComponentChanges(); |
||||
} |
expect(spectator.query('abp-dynamic-layout')).toBeTruthy(); |
||||
return null; |
expect(spectator.query('abp-layout-application')).toBeTruthy(); |
||||
}), |
}); |
||||
}, |
|
||||
}, |
it('should handle account layout from own property and display it', async () => { |
||||
], |
spectator.router.navigateByUrl('/parentWithLayout/childWithLayout'); |
||||
imports: [RouterModule, DummyComponent, DynamicLayoutComponent, ...LAYOUTS], |
await spectator.fixture.whenStable(); |
||||
routes: [ |
spectator.detectComponentChanges(); |
||||
{ path: '', component: RouterOutletComponent }, |
expect(spectator.query('abp-layout-account')).toBeTruthy(); |
||||
{ |
}); |
||||
path: 'parentWithLayout', |
|
||||
component: DynamicLayoutComponent, |
it('should handle empty layout from route data and display it', async () => { |
||||
children: [ |
spectator.router.navigateByUrl('/withData'); |
||||
{ |
await spectator.fixture.whenStable(); |
||||
path: 'childWithoutLayout', |
spectator.detectComponentChanges(); |
||||
component: DummyComponent, |
expect(spectator.query('abp-layout-empty')).toBeTruthy(); |
||||
data: { name: 'childWithoutLayout' }, |
}); |
||||
}, |
|
||||
{ |
it('should display empty layout when layout is null', async () => { |
||||
path: 'childWithLayout', |
spectator.router.navigateByUrl('/withoutLayout'); |
||||
component: DummyComponent, |
await spectator.fixture.whenStable(); |
||||
data: { name: 'childWithLayout' }, |
spectator.detectComponentChanges(); |
||||
}, |
expect(spectator.query('abp-layout-empty')).toBeTruthy(); |
||||
], |
}); |
||||
}, |
|
||||
{ |
it('should not display any layout when layouts are empty', async () => { |
||||
path: 'withData', |
const spy = jest.spyOn(replaceableComponents, 'get'); |
||||
component: DynamicLayoutComponent, |
spy.mockReturnValue(null); |
||||
children: [ |
spectator.detectChanges(); |
||||
{ |
|
||||
path: '', |
spectator.router.navigateByUrl('/withoutLayout'); |
||||
component: DummyComponent, |
await spectator.fixture.whenStable(); |
||||
data: { name: 'withData' }, |
spectator.detectComponentChanges(); |
||||
}, |
|
||||
], |
expect(spectator.query('abp-layout-empty')).toBeFalsy(); |
||||
data: { layout: eLayoutType.empty }, |
}); |
||||
}, |
}); |
||||
{ |
|
||||
path: 'withoutLayout', |
|
||||
component: DynamicLayoutComponent, |
|
||||
children: [ |
|
||||
{ |
|
||||
path: '', |
|
||||
component: DummyComponent, |
|
||||
data: { name: 'withoutLayout' }, |
|
||||
}, |
|
||||
], |
|
||||
data: { layout: null }, |
|
||||
}, |
|
||||
], |
|
||||
}); |
|
||||
|
|
||||
let spectator: SpectatorRouting<DynamicLayoutComponent>; |
|
||||
let replaceableComponents: ReplaceableComponentsService; |
|
||||
|
|
||||
beforeEach(async () => { |
|
||||
spectator = createComponent(); |
|
||||
replaceableComponents = spectator.inject(ReplaceableComponentsService); |
|
||||
const routesService = spectator.inject(RoutesService); |
|
||||
routesService.add(routes); |
|
||||
|
|
||||
replaceableComponents.add({ |
|
||||
key: 'Theme.ApplicationLayoutComponent', |
|
||||
component: DummyApplicationLayoutComponent, |
|
||||
}); |
|
||||
replaceableComponents.add({ |
|
||||
key: 'Theme.AccountLayoutComponent', |
|
||||
component: DummyAccountLayoutComponent, |
|
||||
}); |
|
||||
replaceableComponents.add({ |
|
||||
key: 'Theme.EmptyLayoutComponent', |
|
||||
component: DummyEmptyLayoutComponent, |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
it('should handle application layout from parent abp route and display it', async () => { |
|
||||
spectator.router.navigateByUrl('/parentWithLayout/childWithoutLayout'); |
|
||||
await spectator.fixture.whenStable(); |
|
||||
await new Promise(resolve => setTimeout(resolve, 100)); |
|
||||
spectator.detectComponentChanges(); |
|
||||
expect(spectator.query('abp-dynamic-layout')).toBeTruthy(); |
|
||||
expect(spectator.query('abp-layout-application')).toBeTruthy(); |
|
||||
}); |
|
||||
|
|
||||
it('should handle account layout from own property and display it', async () => { |
|
||||
spectator.router.navigateByUrl('/parentWithLayout/childWithLayout'); |
|
||||
await spectator.fixture.whenStable(); |
|
||||
await new Promise(resolve => setTimeout(resolve, 100)); |
|
||||
spectator.detectComponentChanges(); |
|
||||
expect(spectator.query('abp-layout-account')).toBeTruthy(); |
|
||||
}); |
|
||||
|
|
||||
it('should handle empty layout from route data and display it', async () => { |
|
||||
spectator.router.navigateByUrl('/withData'); |
|
||||
await spectator.fixture.whenStable(); |
|
||||
await new Promise(resolve => setTimeout(resolve, 100)); |
|
||||
spectator.detectComponentChanges(); |
|
||||
expect(spectator.query('abp-layout-empty')).toBeTruthy(); |
|
||||
}); |
|
||||
|
|
||||
it('should display empty layout when layout is null', async () => { |
|
||||
spectator.router.navigateByUrl('/withoutLayout'); |
|
||||
await spectator.fixture.whenStable(); |
|
||||
await new Promise(resolve => setTimeout(resolve, 100)); |
|
||||
spectator.detectComponentChanges(); |
|
||||
expect(spectator.query('abp-layout-empty')).toBeTruthy(); |
|
||||
}); |
|
||||
|
|
||||
it('should handle layout not found scenario', async () => { |
|
||||
spectator.router.navigateByUrl('/withoutLayout'); |
|
||||
await spectator.fixture.whenStable(); |
|
||||
await new Promise(resolve => setTimeout(resolve, 100)); |
|
||||
spectator.detectComponentChanges(); |
|
||||
|
|
||||
expect(spectator.query('abp-dynamic-layout')).toBeTruthy(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|||||
@ -1,284 +1,282 @@ |
|||||
import { TestBed } from '@angular/core/testing'; |
import { Injector } from '@angular/core'; |
||||
import { of } from 'rxjs'; |
import { Router } from '@angular/router'; |
||||
import { LocalizationService } from '../services/localization.service'; |
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; |
||||
|
import { BehaviorSubject } from 'rxjs'; |
||||
|
import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; |
||||
import { ConfigStateService } from '../services/config-state.service'; |
import { ConfigStateService } from '../services/config-state.service'; |
||||
import { SessionStateService } from '../services/session-state.service'; |
import { SessionStateService } from '../services/session-state.service'; |
||||
|
import { LocalizationService } from '../services/localization.service'; |
||||
import { CORE_OPTIONS } from '../tokens/options.token'; |
import { CORE_OPTIONS } from '../tokens/options.token'; |
||||
import { ABP } from '../models/common'; |
import { CONFIG_STATE_DATA } from './config-state.service.spec'; |
||||
|
import { AbpApplicationLocalizationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-localization.service'; |
||||
|
import { APPLICATION_LOCALIZATION_DATA } from './application-localization.service.spec'; |
||||
|
import { IncludeLocalizationResourcesProvider } from '../providers'; |
||||
|
|
||||
|
const appConfigData$ = new BehaviorSubject(CONFIG_STATE_DATA); |
||||
|
const appLocalizationData$ = new BehaviorSubject(APPLICATION_LOCALIZATION_DATA); |
||||
|
|
||||
describe('LocalizationService', () => { |
describe('LocalizationService', () => { |
||||
|
let spectator: SpectatorService<LocalizationService>; |
||||
|
let sessionState: SpyObject<SessionStateService>; |
||||
|
let configState: SpyObject<ConfigStateService>; |
||||
let service: LocalizationService; |
let service: LocalizationService; |
||||
let sessionState: SessionStateService; |
|
||||
let configState: ConfigStateService; |
|
||||
|
|
||||
const mockLocalizationData = { |
const createService = createServiceFactory({ |
||||
defaultResourceName: 'MyProjectName', |
service: LocalizationService, |
||||
values: { |
entryComponents: [], |
||||
MyProjectName: { |
mocks: [Router], |
||||
'Welcome': 'Welcome', |
providers: [ |
||||
'Hello {0}': 'Hello {0}', |
IncludeLocalizationResourcesProvider, |
||||
|
{ |
||||
|
provide: CORE_OPTIONS, |
||||
|
useValue: { registerLocaleFn: () => Promise.resolve(), cultureNameLocaleFileMap: {} }, |
||||
}, |
}, |
||||
AbpIdentity: { |
{ |
||||
'Identity': 'Identity', |
provide: AbpApplicationConfigurationService, |
||||
'User': 'User', |
useValue: { get: () => appConfigData$ }, |
||||
}, |
}, |
||||
}, |
{ |
||||
}; |
provide: AbpApplicationLocalizationService, |
||||
|
useValue: { get: () => appLocalizationData$ }, |
||||
const mockLocalizationsMap = new Map<string, Record<string, string>>(); |
}, |
||||
mockLocalizationsMap.set('AbpIdentity', { 'Identity': 'Identity', 'User': 'User' }); |
], |
||||
mockLocalizationsMap.set('MyProjectName', { 'Welcome': 'Welcome', 'Hello {0}': 'Hello {0}' }); |
}); |
||||
|
|
||||
beforeEach(() => { |
beforeEach(() => { |
||||
TestBed.configureTestingModule({ |
spectator = createService(); |
||||
providers: [ |
sessionState = spectator.inject(SessionStateService); |
||||
LocalizationService, |
configState = spectator.inject(ConfigStateService); |
||||
{ |
service = spectator.service; |
||||
provide: CORE_OPTIONS, |
|
||||
useValue: { |
|
||||
registerLocaleFn: () => Promise.resolve(), |
|
||||
cultureNameLocaleFileMap: {} |
|
||||
}, |
|
||||
}, |
|
||||
{ |
|
||||
provide: ConfigStateService, |
|
||||
useValue: { |
|
||||
refreshAppState: jest.fn(), |
|
||||
getDeep: jest.fn().mockReturnValue({ |
|
||||
localization: { |
|
||||
currentCulture: { cultureName: 'tr' }, |
|
||||
defaultResourceName: 'MyProjectName', |
|
||||
values: mockLocalizationData.values, |
|
||||
}, |
|
||||
}), |
|
||||
getDeep$: jest.fn().mockReturnValue(of({ |
|
||||
localization: { |
|
||||
currentCulture: { cultureName: 'tr' }, |
|
||||
defaultResourceName: 'MyProjectName', |
|
||||
values: mockLocalizationData.values, |
|
||||
}, |
|
||||
})), |
|
||||
getOne: jest.fn().mockReturnValue(mockLocalizationData), |
|
||||
getOne$: jest.fn().mockReturnValue(of(mockLocalizationData)), |
|
||||
getAll: jest.fn().mockReturnValue({ |
|
||||
localization: mockLocalizationData, |
|
||||
}), |
|
||||
getAll$: jest.fn().mockReturnValue(of({ |
|
||||
localization: mockLocalizationData, |
|
||||
})), |
|
||||
refreshLocalization: jest.fn().mockReturnValue(of({})), |
|
||||
}, |
|
||||
}, |
|
||||
{ |
|
||||
provide: SessionStateService, |
|
||||
useValue: { |
|
||||
setLanguage: jest.fn(), |
|
||||
setTenant: jest.fn(), |
|
||||
setInitialLanguage: jest.fn(), |
|
||||
getLanguage: jest.fn().mockReturnValue('tr'), |
|
||||
getTenant: jest.fn().mockReturnValue(null), |
|
||||
getInitialLanguage: jest.fn().mockReturnValue('tr'), |
|
||||
onLanguageChange$: jest.fn().mockReturnValue(of('tr')), |
|
||||
getLanguage$: jest.fn().mockReturnValue(of('tr')), |
|
||||
}, |
|
||||
}, |
|
||||
], |
|
||||
}); |
|
||||
service = TestBed.inject(LocalizationService); |
|
||||
sessionState = TestBed.inject(SessionStateService); |
|
||||
configState = TestBed.inject(ConfigStateService); |
|
||||
|
|
||||
(service as any).localizations$.next(mockLocalizationsMap); |
configState.refreshAppState(); |
||||
|
sessionState.setLanguage('tr'); |
||||
|
appConfigData$.next(CONFIG_STATE_DATA); |
||||
}); |
}); |
||||
|
|
||||
describe('#currentLang', () => { |
describe('#currentLang', () => { |
||||
it('should return current language', () => { |
it('should be tr', done => { |
||||
expect(service.currentLang).toBe('tr'); |
setTimeout(() => { |
||||
}); |
expect(service.currentLang).toBe('tr'); |
||||
|
|
||||
it('should return observable of current language', (done) => { |
|
||||
service.currentLang$.subscribe(lang => { |
|
||||
expect(lang).toBe('tr'); |
|
||||
done(); |
done(); |
||||
}); |
}, 0); |
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('#languageChange$', () => { |
|
||||
it('should emit language changes', (done) => { |
|
||||
service.languageChange$.subscribe(lang => { |
|
||||
expect(lang).toBe('tr'); |
|
||||
done(); |
|
||||
}); |
|
||||
|
|
||||
(service as any)._languageChange$.next('tr'); |
|
||||
}); |
}); |
||||
}); |
}); |
||||
|
|
||||
describe('#get', () => { |
describe('#get', () => { |
||||
it('should return observable localization for valid key', (done) => { |
it('should be return an observable localization', done => { |
||||
service.get('AbpIdentity::Identity').subscribe(result => { |
service.get('AbpIdentity::Identity').subscribe(localization => { |
||||
expect(result).toBe('Identity'); |
expect(localization).toBe(CONFIG_STATE_DATA.localization.values.AbpIdentity.Identity); |
||||
done(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
it('should return key when localization not found', (done) => { |
|
||||
service.get('AbpIdentity::NonExistent').subscribe(result => { |
|
||||
expect(result).toBe('NonExistent'); |
|
||||
done(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
it('should handle interpolation', (done) => { |
|
||||
service.get('MyProjectName::Hello {0}', 'John').subscribe(result => { |
|
||||
expect(result).toBe('Hello John'); |
|
||||
done(); |
done(); |
||||
}); |
}); |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|
||||
describe('#instant', () => { |
describe('#instant', () => { |
||||
it('should return localization for valid key', () => { |
it('should be return a localization', () => { |
||||
const result = service.instant('AbpIdentity::Identity'); |
const localization = service.instant('AbpIdentity::Identity'); |
||||
expect(result).toBe('Identity'); |
|
||||
}); |
|
||||
|
|
||||
it('should return key when localization not found', () => { |
expect(localization).toBe(CONFIG_STATE_DATA.localization.values.AbpIdentity.Identity); |
||||
const result = service.instant('AbpIdentity::NonExistent'); |
|
||||
expect(result).toBe('NonExistent'); |
|
||||
}); |
}); |
||||
|
}); |
||||
|
|
||||
it('should handle interpolation', () => { |
describe('#registerLocale', () => { |
||||
const result = service.instant('MyProjectName::Hello {0}', 'John'); |
it('should throw an error message when service have an otherInstance', async () => { |
||||
expect(result).toBe('Hello John'); |
try { |
||||
|
const instance = new LocalizationService( |
||||
|
sessionState, |
||||
|
spectator.inject(Injector), |
||||
|
null, |
||||
|
configState, |
||||
|
); |
||||
|
} catch (error) { |
||||
|
expect((error as Error).message).toBe('LocalizationService should have only one instance.'); |
||||
|
} |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|
||||
describe('#localize', () => { |
describe('#localize', () => { |
||||
it('should return observable localization for valid resource and key', (done) => { |
test.each` |
||||
service.localize('AbpIdentity', 'Identity', 'Default').subscribe(result => { |
resource | key | defaultValue | expected |
||||
expect(result).toBe('Identity'); |
${'_'} | ${'TEST'} | ${'DEFAULT'} | ${'TEST'} |
||||
done(); |
${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} |
||||
}); |
${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
it('should return default value when key not found', (done) => { |
${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
service.localize('AbpIdentity', 'NonExistent', 'Default').subscribe(result => { |
${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
expect(result).toBe('Default'); |
${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} |
||||
done(); |
${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
`(
|
||||
|
'should return observable $expected when resource name is $resource and key is $key', |
||||
|
async ({ resource, key, defaultValue, expected }) => { |
||||
|
appConfigData$.next({ |
||||
|
localization: { |
||||
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
||||
|
defaultResourceName: 'x', |
||||
|
}, |
||||
|
} as any); |
||||
|
configState.refreshAppState(); |
||||
|
|
||||
it('should return default value when resource not found', (done) => { |
service.localize(resource, key, defaultValue).subscribe(result => { |
||||
service.localize('NonExistent', 'Identity', 'Default').subscribe(result => { |
expect(result).toBe(expected); |
||||
expect(result).toBe('Default'); |
}); |
||||
done(); |
}, |
||||
}); |
); |
||||
}); |
|
||||
}); |
}); |
||||
|
|
||||
describe('#localizeSync', () => { |
describe('#localizeSync', () => { |
||||
it('should return localization for valid resource and key', () => { |
test.each` |
||||
const result = service.localizeSync('AbpIdentity', 'Identity', 'Default'); |
resource | key | defaultValue | expected |
||||
expect(result).toBe('Identity'); |
${'_'} | ${'TEST'} | ${'DEFAULT'} | ${'TEST'} |
||||
}); |
${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} |
||||
|
${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} |
||||
|
${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
`(
|
||||
|
'should return $expected when resource name is $resource and key is $key', |
||||
|
({ resource, key, defaultValue, expected }) => { |
||||
|
appConfigData$.next({ |
||||
|
localization: { |
||||
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
||||
|
defaultResourceName: 'x', |
||||
|
}, |
||||
|
} as any); |
||||
|
configState.refreshAppState(); |
||||
|
|
||||
it('should return default value when key not found', () => { |
const result = service.localizeSync(resource, key, defaultValue); |
||||
const result = service.localizeSync('AbpIdentity', 'NonExistent', 'Default'); |
|
||||
expect(result).toBe('Default'); |
|
||||
}); |
|
||||
|
|
||||
it('should return default value when resource not found', () => { |
expect(result).toBe(expected); |
||||
const result = service.localizeSync('NonExistent', 'Identity', 'Default'); |
}, |
||||
expect(result).toBe('Default'); |
); |
||||
}); |
|
||||
}); |
}); |
||||
|
|
||||
describe('#localizeWithFallback', () => { |
describe('#localizeWithFallback', () => { |
||||
it('should return observable localization from first available resource', (done) => { |
test.each` |
||||
service.localizeWithFallback(['AbpIdentity', 'MyProjectName'], ['Identity'], 'Default').subscribe(result => { |
resources | keys | defaultValue | expected |
||||
expect(result).toBe('Identity'); |
${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'} |
||||
done(); |
${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} |
||||
}); |
${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} |
||||
|
${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
`(
|
||||
|
'should return observable $expected when resource names are $resources and keys are $keys', |
||||
|
async ({ resources, keys, defaultValue, expected }) => { |
||||
|
appConfigData$.next({ |
||||
|
localization: { |
||||
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
||||
|
defaultResourceName: 'x', |
||||
|
}, |
||||
|
} as any); |
||||
|
configState.refreshAppState(); |
||||
|
|
||||
it('should return default value when no resource has the key', (done) => { |
service.localizeWithFallback(resources, keys, defaultValue).subscribe(result => { |
||||
service.localizeWithFallback(['AbpIdentity', 'MyProjectName'], ['NonExistent'], 'Default').subscribe(result => { |
expect(result).toBe(expected); |
||||
expect(result).toBe('Default'); |
}); |
||||
done(); |
}, |
||||
}); |
); |
||||
}); |
|
||||
}); |
}); |
||||
|
|
||||
describe('#localizeWithFallbackSync', () => { |
describe('#localizeWithFallbackSync', () => { |
||||
it('should return localization from first available resource', () => { |
test.each` |
||||
const result = service.localizeWithFallbackSync(['AbpIdentity', 'MyProjectName'], ['Identity'], 'Default'); |
resources | keys | defaultValue | expected |
||||
expect(result).toBe('Identity'); |
${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'} |
||||
}); |
${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} |
||||
|
${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
it('should return default value when no resource has the key', () => { |
${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
const result = service.localizeWithFallbackSync(['AbpIdentity', 'MyProjectName'], ['NonExistent'], 'Default'); |
${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
expect(result).toBe('Default'); |
${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
}); |
${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
describe('#getResource', () => { |
${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
it('should return resource for valid resource name', () => { |
${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} |
||||
const resource = service.getResource('AbpIdentity'); |
${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} |
||||
expect(resource).toBeDefined(); |
${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
expect(resource?.['Identity']).toBe('Identity'); |
${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
}); |
${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
|
${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
||||
it('should return undefined for non-existent resource', () => { |
${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
const resource = service.getResource('NonExistent'); |
${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
expect(resource).toBeUndefined(); |
${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
}); |
${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
|
${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
describe('#getResource$', () => { |
${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
it('should return observable resource for valid resource name', (done) => { |
${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
service.getResource$('AbpIdentity').subscribe(resource => { |
${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
expect(resource).toBeDefined(); |
${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
||||
expect(resource?.['Identity']).toBe('Identity'); |
`(
|
||||
done(); |
'should return $expected when resource names are $resources and keys are $keys', |
||||
}); |
({ resources, keys, defaultValue, expected }) => { |
||||
}); |
appConfigData$.next({ |
||||
|
localization: { |
||||
it('should return observable undefined for non-existent resource', (done) => { |
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
||||
service.getResource$('NonExistent').subscribe(resource => { |
defaultResourceName: 'x', |
||||
expect(resource).toBeUndefined(); |
}, |
||||
done(); |
} as any); |
||||
}); |
configState.refreshAppState(); |
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('#addLocalization', () => { |
const result = service.localizeWithFallbackSync(resources, keys, defaultValue); |
||||
it('should add localization data', () => { |
|
||||
const localizations: ABP.Localization[] = [ |
|
||||
{ |
|
||||
culture: 'en', |
|
||||
resources: [ |
|
||||
{ |
|
||||
resourceName: 'TestResource', |
|
||||
texts: { |
|
||||
'TestKey': 'TestValue', |
|
||||
}, |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
]; |
|
||||
|
|
||||
service.addLocalization(localizations); |
expect(result).toBe(expected); |
||||
|
}, |
||||
expect(() => service.addLocalization(localizations)).not.toThrow(); |
); |
||||
}); |
|
||||
}); |
}); |
||||
|
|
||||
describe('#registerLocale', () => { |
describe('#getLocalization', () => { |
||||
it('should register locale successfully', async () => { |
it('should return a localization', () => { |
||||
const result = await service.registerLocale('en'); |
expect( |
||||
expect(result).toBeUndefined(); |
service.instant("MyProjectName::'{0}' and '{1}' do not match.", 'first', 'second'), |
||||
|
).toBe('first and second do not match.'); |
||||
}); |
}); |
||||
}); |
}); |
||||
}); |
}); |
||||
|
|||||
@ -1,176 +1,174 @@ |
|||||
import { Component, EventEmitter, Input, Output, inject } from '@angular/core'; |
import { Component, EventEmitter, Input, Output, inject } from '@angular/core'; |
||||
import { Router } from '@angular/router'; |
import { Router } from '@angular/router'; |
||||
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator/jest'; |
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator/jest'; |
||||
import { BehaviorSubject } from 'rxjs'; |
import { BehaviorSubject } from 'rxjs'; |
||||
import { ReplaceableTemplateDirective } from '../directives/replaceable-template.directive'; |
import { ReplaceableTemplateDirective } from '../directives/replaceable-template.directive'; |
||||
import { ReplaceableComponents } from '../models/replaceable-components'; |
import { ReplaceableComponents } from '../models/replaceable-components'; |
||||
import { ReplaceableComponentsService } from '../services/replaceable-components.service'; |
import { ReplaceableComponentsService } from '../services/replaceable-components.service'; |
||||
|
|
||||
@Component({ |
@Component({ |
||||
selector: 'abp-default-component', |
selector: 'abp-default-component', |
||||
template: ' <p>default</p> ', |
template: ' <p>default</p> ', |
||||
exportAs: 'abpDefaultComponent', |
exportAs: 'abpDefaultComponent', |
||||
standalone: true, |
}) |
||||
}) |
class DefaultComponent { |
||||
class DefaultComponent { |
@Input() |
||||
@Input() |
oneWay; |
||||
oneWay; |
|
||||
|
@Input() |
||||
@Input() |
twoWay: boolean; |
||||
twoWay: boolean; |
|
||||
|
@Output() |
||||
@Output() |
readonly twoWayChange = new EventEmitter<boolean>(); |
||||
readonly twoWayChange = new EventEmitter<boolean>(); |
|
||||
|
@Output() |
||||
@Output() |
readonly someOutput = new EventEmitter<string>(); |
||||
readonly someOutput = new EventEmitter<string>(); |
|
||||
|
setTwoWay(value) { |
||||
setTwoWay(value) { |
this.twoWay = value; |
||||
this.twoWay = value; |
this.twoWayChange.emit(value); |
||||
this.twoWayChange.emit(value); |
} |
||||
} |
} |
||||
} |
|
||||
|
@Component({ |
||||
@Component({ |
selector: 'abp-external-component', |
||||
selector: 'abp-external-component', |
template: ' <p>external</p> ', |
||||
template: ' <p>external</p> ', |
}) |
||||
standalone: true, |
class ExternalComponent {
data = inject<ReplaceableComponents.ReplaceableTemplateData<any, any>>('REPLACEABLE_DATA' as any, { optional: true })!; |
||||
}) |
|
||||
class ExternalComponent { |
} |
||||
data = inject<ReplaceableComponents.ReplaceableTemplateData<any, any>>('REPLACEABLE_DATA' as any, { optional: true })!; |
|
||||
|
describe('ReplaceableTemplateDirective', () => { |
||||
} |
let spectator: SpectatorDirective<ReplaceableTemplateDirective>; |
||||
|
const get$Res = new BehaviorSubject(undefined); |
||||
describe('ReplaceableTemplateDirective', () => { |
|
||||
let spectator: SpectatorDirective<ReplaceableTemplateDirective>; |
const createDirective = createDirectiveFactory({ |
||||
const get$Res = new BehaviorSubject(undefined); |
directive: ReplaceableTemplateDirective, |
||||
|
declarations: [DefaultComponent, ExternalComponent], |
||||
const createDirective = createDirectiveFactory({ |
entryComponents: [ExternalComponent], |
||||
directive: ReplaceableTemplateDirective, |
mocks: [Router], |
||||
imports: [DefaultComponent, ExternalComponent], |
providers: [{ provide: ReplaceableComponentsService, useValue: { get$: () => get$Res } }], |
||||
mocks: [Router], |
}); |
||||
providers: [{ provide: ReplaceableComponentsService, useValue: { get$: () => get$Res } }], |
|
||||
}); |
describe('without external component', () => { |
||||
|
const twoWayChange = jest.fn(a => a); |
||||
describe('without external component', () => { |
const someOutput = jest.fn(a => a); |
||||
const twoWayChange = jest.fn(a => a); |
|
||||
const someOutput = jest.fn(a => a); |
beforeEach(() => { |
||||
|
spectator = createDirective( |
||||
beforeEach(() => { |
` |
||||
spectator = createDirective( |
<div *abpReplaceableTemplate="{inputs: {oneWay: {value: oneWay}, twoWay: {value: twoWay, twoWay: true}}, outputs: {twoWayChange: twoWayChange, someOutput: someOutput}, componentKey: 'TestModule.TestComponent'}; let initTemplate = initTemplate"> |
||||
` |
<abp-default-component #defaultComponent="abpDefaultComponent"></abp-default-component> |
||||
<div *abpReplaceableTemplate="{inputs: {oneWay: {value: oneWay}, twoWay: {value: twoWay, twoWay: true}}, outputs: {twoWayChange: twoWayChange, someOutput: someOutput}, componentKey: 'TestModule.TestComponent'}; let initTemplate = initTemplate"> |
</div> |
||||
<abp-default-component #defaultComponent="abpDefaultComponent"></abp-default-component> |
`,
|
||||
</div> |
{ |
||||
`,
|
hostProps: { |
||||
{ |
oneWay: { label: 'Test' }, |
||||
hostProps: { |
twoWay: false, |
||||
oneWay: { label: 'Test' }, |
twoWayChange, |
||||
twoWay: false, |
someOutput, |
||||
twoWayChange, |
}, |
||||
someOutput, |
}, |
||||
}, |
); |
||||
}, |
|
||||
); |
const component = spectator.query(DefaultComponent); |
||||
|
spectator.directive.context.initTemplate(component); |
||||
const component = spectator.query(DefaultComponent); |
spectator.detectChanges(); |
||||
spectator.directive.context.initTemplate(component); |
}); |
||||
spectator.detectChanges(); |
|
||||
}); |
afterEach(() => twoWayChange.mockClear()); |
||||
|
|
||||
afterEach(() => twoWayChange.mockClear()); |
it('should display the default template when store response is undefined', () => { |
||||
|
expect(spectator.query('abp-default-component')).toBeTruthy(); |
||||
it('should display the default template when store response is undefined', () => { |
}); |
||||
expect(spectator.query('abp-default-component')).toBeTruthy(); |
|
||||
}); |
it('should be setted inputs and outputs', () => { |
||||
|
const component = spectator.query(DefaultComponent); |
||||
it('should be setted inputs and outputs', () => { |
expect(component.oneWay).toEqual({ label: 'Test' }); |
||||
const component = spectator.query(DefaultComponent); |
expect(component.twoWay).toEqual(false); |
||||
expect(component.oneWay).toEqual({ label: 'Test' }); |
}); |
||||
expect(component.twoWay).toEqual(false); |
|
||||
}); |
it('should change the component inputs', () => { |
||||
|
const component = spectator.query(DefaultComponent); |
||||
it('should change the component inputs', () => { |
spectator.setHostInput({ oneWay: 'test' }); |
||||
const component = spectator.query(DefaultComponent); |
component.setTwoWay(true); |
||||
spectator.setHostInput({ oneWay: 'test' }); |
component.someOutput.emit('someOutput emitted'); |
||||
component.setTwoWay(true); |
expect(component.oneWay).toBe('test'); |
||||
component.someOutput.emit('someOutput emitted'); |
expect(twoWayChange).toHaveBeenCalledWith(true); |
||||
expect(component.oneWay).toBe('test'); |
expect(someOutput).toHaveBeenCalledWith('someOutput emitted'); |
||||
expect(twoWayChange).toHaveBeenCalledWith(true); |
}); |
||||
expect(someOutput).toHaveBeenCalledWith('someOutput emitted'); |
}); |
||||
}); |
|
||||
}); |
describe('with external component', () => { |
||||
|
const twoWayChange = jest.fn(a => a); |
||||
describe('with external component', () => { |
const someOutput = jest.fn(a => a); |
||||
const twoWayChange = jest.fn(a => a); |
|
||||
const someOutput = jest.fn(a => a); |
beforeEach(() => { |
||||
|
spectator = createDirective( |
||||
beforeEach(() => { |
` |
||||
spectator = createDirective( |
<div *abpReplaceableTemplate="{inputs: {oneWay: {value: oneWay}, twoWay: {value: twoWay, twoWay: true}}, outputs: {twoWayChange: twoWayChange, someOutput: someOutput}, componentKey: 'TestModule.TestComponent'}; let initTemplate = initTemplate"> |
||||
` |
<abp-default-component #defaultComponent="abpDefaultComponent"></abp-default-component> |
||||
<div *abpReplaceableTemplate="{inputs: {oneWay: {value: oneWay}, twoWay: {value: twoWay, twoWay: true}}, outputs: {twoWayChange: twoWayChange, someOutput: someOutput}, componentKey: 'TestModule.TestComponent'}; let initTemplate = initTemplate"> |
</div> |
||||
<abp-default-component #defaultComponent="abpDefaultComponent"></abp-default-component> |
`,
|
||||
</div> |
{ hostProps: { oneWay: { label: 'Test' }, twoWay: false, twoWayChange, someOutput } }, |
||||
`,
|
); |
||||
{ hostProps: { oneWay: { label: 'Test' }, twoWay: false, twoWayChange, someOutput } }, |
|
||||
); |
get$Res.next({ component: ExternalComponent, key: 'TestModule.TestComponent' }); |
||||
|
}); |
||||
get$Res.next({ component: ExternalComponent, key: 'TestModule.TestComponent' }); |
|
||||
}); |
afterEach(() => twoWayChange.mockClear()); |
||||
|
|
||||
afterEach(() => twoWayChange.mockClear()); |
it('should display the external component', () => { |
||||
|
expect(spectator.query('p')).toHaveText('external'); |
||||
it('should display the external component', () => { |
}); |
||||
expect(spectator.query('p')).toHaveText('external'); |
|
||||
}); |
it('should be injected the data object', () => { |
||||
|
const externalComponent = spectator.query(ExternalComponent); |
||||
it('should be injected the data object', () => { |
expect(externalComponent.data).toEqual({ |
||||
const externalComponent = spectator.query(ExternalComponent); |
componentKey: 'TestModule.TestComponent', |
||||
expect(externalComponent.data).toEqual({ |
inputs: { oneWay: { label: 'Test' }, twoWay: false }, |
||||
componentKey: 'TestModule.TestComponent', |
outputs: { someOutput, twoWayChange }, |
||||
inputs: { oneWay: { label: 'Test' }, twoWay: false }, |
}); |
||||
outputs: { someOutput, twoWayChange }, |
}); |
||||
}); |
|
||||
}); |
it('should be worked all data properties', () => { |
||||
|
const externalComponent = spectator.query(ExternalComponent); |
||||
it('should be worked all data properties', () => { |
spectator.setHostInput({ oneWay: 'test' }); |
||||
const externalComponent = spectator.query(ExternalComponent); |
externalComponent.data.inputs.twoWay = true; |
||||
spectator.setHostInput({ oneWay: 'test' }); |
externalComponent.data.outputs.someOutput('someOutput emitted'); |
||||
externalComponent.data.inputs.twoWay = true; |
expect(externalComponent.data.inputs.oneWay).toBe('test'); |
||||
externalComponent.data.outputs.someOutput('someOutput emitted'); |
expect(twoWayChange).toHaveBeenCalledWith(true); |
||||
expect(externalComponent.data.inputs.oneWay).toBe('test'); |
expect(someOutput).toHaveBeenCalledWith('someOutput emitted'); |
||||
expect(twoWayChange).toHaveBeenCalledWith(true); |
|
||||
expect(someOutput).toHaveBeenCalledWith('someOutput emitted'); |
spectator.setHostInput({ twoWay: 'twoWay test' }); |
||||
|
expect(externalComponent.data.inputs.twoWay).toBe('twoWay test'); |
||||
spectator.setHostInput({ twoWay: 'twoWay test' }); |
}); |
||||
expect(externalComponent.data.inputs.twoWay).toBe('twoWay test'); |
|
||||
}); |
it('should be worked correctly the default component when the external component has been removed from store', () => { |
||||
|
expect(spectator.query('p')).toHaveText('external'); |
||||
it('should be worked correctly the default component when the external component has been removed from store', () => { |
const externalComponent = spectator.query(ExternalComponent); |
||||
expect(spectator.query('p')).toHaveText('external'); |
spectator.setHostInput({ oneWay: 'test' }); |
||||
const externalComponent = spectator.query(ExternalComponent); |
externalComponent.data.inputs.twoWay = true; |
||||
spectator.setHostInput({ oneWay: 'test' }); |
get$Res.next({ component: null, key: 'TestModule.TestComponent' }); |
||||
externalComponent.data.inputs.twoWay = true; |
spectator.detectChanges(); |
||||
get$Res.next({ component: null, key: 'TestModule.TestComponent' }); |
const component = spectator.query(DefaultComponent); |
||||
spectator.detectChanges(); |
spectator.directive.context.initTemplate(component); |
||||
const component = spectator.query(DefaultComponent); |
expect(spectator.query('abp-default-component')).toBeTruthy(); |
||||
spectator.directive.context.initTemplate(component); |
|
||||
expect(spectator.query('abp-default-component')).toBeTruthy(); |
expect(component.oneWay).toEqual('test'); |
||||
|
expect(component.twoWay).toEqual(true); |
||||
expect(component.oneWay).toEqual('test'); |
}); |
||||
expect(component.twoWay).toEqual(true); |
|
||||
}); |
it('should reset default component subscriptions', () => { |
||||
|
get$Res.next({ component: null, key: 'TestModule.TestComponent' }); |
||||
it('should reset default component subscriptions', () => { |
const component = spectator.query(DefaultComponent); |
||||
get$Res.next({ component: null, key: 'TestModule.TestComponent' }); |
spectator.directive.context.initTemplate(component); |
||||
const component = spectator.query(DefaultComponent); |
spectator.detectChanges(); |
||||
spectator.directive.context.initTemplate(component); |
const unsubscribe = jest.fn(() => {}); |
||||
spectator.detectChanges(); |
spectator.directive.defaultComponentSubscriptions.twoWayChange.unsubscribe = unsubscribe; |
||||
const unsubscribe = jest.fn(() => {}); |
|
||||
spectator.directive.defaultComponentSubscriptions.twoWayChange.unsubscribe = unsubscribe; |
get$Res.next({ component: ExternalComponent, key: 'TestModule.TestComponent' }); |
||||
|
expect(unsubscribe).toHaveBeenCalled(); |
||||
get$Res.next({ component: ExternalComponent, key: 'TestModule.TestComponent' }); |
}); |
||||
expect(unsubscribe).toHaveBeenCalled(); |
}); |
||||
}); |
}); |
||||
}); |
|
||||
}); |
|
||||
|
|||||
Loading…
Reference in new issue