diff --git a/packages/angular/.eslintrc.json b/packages/angular/.eslintrc.json index 358a3529..44e51918 100644 --- a/packages/angular/.eslintrc.json +++ b/packages/angular/.eslintrc.json @@ -47,19 +47,20 @@ "import/no-extraneous-dependencies": [ "error", { - "devDependencies": ["**/*.spec.ts"], - "packageDir": "./" // Needed for angular library support + "devDependencies": [ + "**/*.spec.ts" + ], + "packageDir": "./" } ], "tsdoc/syntax": "warn", - "unicorn/filename-case": "off", // We are naming files with capitals - // I think some of these are silly. `e` is bad, but `err` and `exc` are decent. `user of users` is confusing, `usr of users` is safer. + "unicorn/filename-case": "off", "unicorn/prevent-abbreviations": "off" }, "settings": { "import/resolver": { "typescript": { - "alwaysTryTypes": true // always try to resolve types under `@types` directory even it doesn't contain any source code, like `@types/unist` + "alwaysTryTypes": true } } } diff --git a/packages/angular/angular.json b/packages/angular/angular.json index 48184fa9..83f47bdb 100644 --- a/packages/angular/angular.json +++ b/packages/angular/angular.json @@ -26,7 +26,7 @@ "test": { "builder": "@angular-devkit/build-angular:karma", "options": { - "main": "projects/tx-native-angular-sdk/test.ts", + "polyfills": ["zone.js", "zone.js/testing"], "tsConfig": "projects/tx-native-angular-sdk/tsconfig.spec.json", "karmaConfig": "projects/tx-native-angular-sdk/karma.conf.js", "codeCoverage": true @@ -46,5 +46,12 @@ } } }, - "defaultProject": "@transifex/angular" + "schematics": { + "@angular-eslint/schematics:application": { + "setParserOptionsProject": true + }, + "@angular-eslint/schematics:library": { + "setParserOptionsProject": true + } + } } diff --git a/packages/angular/package.json b/packages/angular/package.json index 98aa7ec3..f8d63b6b 100644 --- a/packages/angular/package.json +++ b/packages/angular/package.json @@ -32,29 +32,29 @@ }, "private": true, "dependencies": { - "@angular/animations": "~14.3.0", - "@angular/common": "~14.3.0", - "@angular/compiler": "~14.3.0", - "@angular/core": "~14.3.0", - "@angular/forms": "~14.3.0", - "@angular/platform-browser": "^14.3.0", - "@angular/platform-browser-dynamic": "^14.3.0", - "@angular/router": "~14.3.0", + "@angular/animations": "~16.2.7", + "@angular/common": "~16.2.7", + "@angular/compiler": "~16.2.7", + "@angular/core": "~16.2.7", + "@angular/forms": "~16.2.7", + "@angular/platform-browser": "^16.2.7", + "@angular/platform-browser-dynamic": "^16.2.7", + "@angular/router": "~16.2.7", "@transifex/native": "^7.0.1", "rxjs": "~6.6.0", "tslib": "^2.6.2", - "zone.js": "^0.11.8" + "zone.js": "^0.13.3" }, "devDependencies": { - "@angular-devkit/architect": "^0.1402.11", - "@angular-devkit/build-angular": "^14.2.13", - "@angular-eslint/builder": "^14.4.0", - "@angular-eslint/eslint-plugin": "^14.4.0", - "@angular-eslint/eslint-plugin-template": "^14.4.0", - "@angular-eslint/schematics": "14.4.0", - "@angular-eslint/template-parser": "^14.4.0", - "@angular/cli": "^14.2.13", - "@angular/compiler-cli": "^14.3.0", + "@angular-devkit/architect": "^0.1602.4", + "@angular-devkit/build-angular": "^16.2.4", + "@angular-eslint/builder": "^16.2.0", + "@angular-eslint/eslint-plugin": "^16.2.0", + "@angular-eslint/eslint-plugin-template": "^16.2.0", + "@angular-eslint/schematics": "16.2.0", + "@angular-eslint/template-parser": "^16.2.0", + "@angular/cli": "^16.2.4", + "@angular/compiler-cli": "^16.2.7", "@types/jasmine": "~4.0.0", "@types/node": "^16.18.70", "@typescript-eslint/eslint-plugin": "^5.62.0", @@ -76,8 +76,8 @@ "karma-coverage": "~2.2.0", "karma-jasmine": "~5.1.0", "karma-jasmine-html-reporter": "^2.1.0", - "ng-packagr": "^14.2.2", + "ng-packagr": "^16.2.3", "ts-node": "~8.3.0", - "typescript": "~4.6.4" + "typescript": "~4.9.5" } } diff --git a/packages/angular/projects/tx-native-angular-sdk/.eslintrc.json b/packages/angular/projects/tx-native-angular-sdk/.eslintrc.json index e442ce2d..411f731b 100644 --- a/packages/angular/projects/tx-native-angular-sdk/.eslintrc.json +++ b/packages/angular/projects/tx-native-angular-sdk/.eslintrc.json @@ -17,7 +17,7 @@ }, "rules": { "@angular-eslint/component-selector": [ - "off", // We are not following the angular styleguide to match other implementations + "off", { "prefix": "tx", "style": "kebab-case", diff --git a/packages/angular/projects/tx-native-angular-sdk/package.json b/packages/angular/projects/tx-native-angular-sdk/package.json index 9b50631a..8970a573 100644 --- a/packages/angular/projects/tx-native-angular-sdk/package.json +++ b/packages/angular/projects/tx-native-angular-sdk/package.json @@ -17,8 +17,8 @@ "repository": "git://github.com/transifex/transifex-javascript.git", "license": "Apache-2.0", "peerDependencies": { - "@angular/common": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0", - "@angular/core": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0", - "@transifex/native": "^7.0.0" + "@angular/common": "^16.0.0", + "@angular/core": " ^16.0.0", + "@transifex/native": "^7.0.1" } } diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/T.component.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/T.component.spec.ts similarity index 72% rename from packages/angular/projects/tx-native-angular-sdk/tests/T.component.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/T.component.spec.ts index b5874d63..0fba8d39 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/T.component.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/T.component.spec.ts @@ -1,10 +1,13 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { ReplaySubject } from 'rxjs'; import { tx } from '@transifex/native'; +import { ReplaySubject } from 'rxjs'; -import { TComponent } from '../src/lib/T.component'; -import { SafeHtmlPipe, TranslationService, TXInstanceComponent } from '../src/public-api'; - +import { TComponent } from '../lib/T.component'; +import { + SafeHtmlPipe, + TranslationService, + TXInstanceComponent, +} from '../public-api'; describe('TComponent', () => { let localeChangedSubject: ReplaySubject; @@ -30,8 +33,7 @@ describe('TComponent', () => { await TestBed.configureTestingModule({ declarations: [TComponent, SafeHtmlPipe, TXInstanceComponent], providers: [TXInstanceComponent], - }) - .compileComponents(); + }).compileComponents(); localeChangedSubject = new ReplaySubject(0); @@ -39,7 +41,11 @@ describe('TComponent', () => { instance = TestBed.inject(TXInstanceComponent); spyOn(service, 'getCurrentLocale').and.returnValue('en'); - localeChangedSpy = spyOnProperty(service, 'localeChanged', 'get').and.returnValue(localeChangedSubject); + localeChangedSpy = spyOnProperty( + service, + 'localeChanged', + 'get' + ).and.returnValue(localeChangedSubject); spyOn(service, 'setCurrentLocale').and.callFake(async (locale) => { localeChangedSubject.next(locale); }); @@ -54,8 +60,11 @@ describe('TComponent', () => { it('should create the component', async () => { // setup spyOn(component, 'translate'); - localeChangedSpy = spyOnProperty(component, 'localeChanged', 'get') - .and.returnValue(localeChangedSubject); + localeChangedSpy = spyOnProperty( + component, + 'localeChanged', + 'get' + ).and.returnValue(localeChangedSubject); // act component.ngOnInit(); @@ -82,8 +91,11 @@ describe('TComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams }, + '' + ); expect(component.translatedStr).toEqual('translated'); }); @@ -98,8 +110,11 @@ describe('TComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams }, + '' + ); expect(component.translatedStr).toEqual('translated'); }); @@ -114,8 +129,11 @@ describe('TComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams, _key: 'key-not-translated' }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams, _key: 'key-not-translated' }, + '' + ); expect(component.translatedStr).toEqual('translated'); }); @@ -130,8 +148,9 @@ describe('TComponent', () => { // assert const compiled = fixture.debugElement.nativeElement; - expect((compiled as HTMLDivElement).innerHTML) - .toContain('<a>translated</a>'); + expect((compiled as HTMLDivElement).innerHTML).toContain( + '<a>translated</a>' + ); }); it('should translate and sanitize the string', () => { @@ -146,8 +165,9 @@ describe('TComponent', () => { // assert const compiled = fixture.debugElement.nativeElement; - expect((compiled as HTMLDivElement).innerHTML) - .toContain('translated'); + expect((compiled as HTMLDivElement).innerHTML).toContain( + 'translated' + ); }); it('should detect input parameters change and translate', () => { @@ -158,14 +178,15 @@ describe('TComponent', () => { // act service.translate('test', { ...translationParams }); component.str = 'other-value'; - component.ngOnChanges() + component.ngOnChanges(); fixture.detectChanges(); // assert expect(service.translate).toHaveBeenCalled(); const compiled = fixture.debugElement.nativeElement; - expect((compiled as HTMLDivElement).innerHTML) - .toContain('<a>translated</a>'); + expect((compiled as HTMLDivElement).innerHTML).toContain( + '<a>translated</a>' + ); }); it('should detect localeChange and translate', async () => { @@ -183,8 +204,11 @@ describe('TComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams, _key: 'key-not-translated' }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams, _key: 'key-not-translated' }, + '' + ); expect(component.translatedStr).toEqual('translated-again'); }); @@ -203,11 +227,15 @@ describe('TComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', { - ...translationParams, - _key: 'key-not-translated', - _context: 'late', - }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { + ...translationParams, + _key: 'key-not-translated', + _context: 'late', + }, + '' + ); expect(component.translatedStr).toEqual('translated'); }); @@ -223,8 +251,11 @@ describe('TComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams }, 'instance-alias'); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams }, + 'instance-alias' + ); expect(component.translatedStr).toEqual('translated'); }); @@ -245,24 +276,26 @@ describe('TComponent', () => { expect(service.translate).toHaveBeenCalled(); }); - it('should detect translationsFetched using alternative instance', - async () => { - // setup - instance.token = 'instance-token'; - instance.alias = 'instance-alias'; - spyOn(service, 'translate').and.returnValue('translated'); - - // act - component.str = 'not-translated'; - component.ngOnInit(); - fixture.detectChanges(); - - // change - await service.fetchTranslations('tag1'); - fixture.detectChanges(); - - // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams }, 'instance-alias'); - }); + it('should detect translationsFetched using alternative instance', async () => { + // setup + instance.token = 'instance-token'; + instance.alias = 'instance-alias'; + spyOn(service, 'translate').and.returnValue('translated'); + + // act + component.str = 'not-translated'; + component.ngOnInit(); + fixture.detectChanges(); + + // change + await service.fetchTranslations('tag1'); + fixture.detectChanges(); + + // assert + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams }, + 'instance-alias' + ); + }); }); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/T.decorator.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/T.decorator.spec.ts similarity index 78% rename from packages/angular/projects/tx-native-angular-sdk/tests/T.decorator.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/T.decorator.spec.ts index 6e9a3cc7..9756b063 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/T.decorator.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/T.decorator.spec.ts @@ -2,7 +2,7 @@ import { Component } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { tx } from '@transifex/native'; -import { T } from '../src/lib/T.decorator'; +import { T } from '../lib/T.decorator'; describe('T Decorator', () => { @Component({ @@ -16,7 +16,12 @@ describe('T Decorator', () => { template: '
{{ testProperty }}
', }) class TestWithInstanceComponent { - @T('not-trans-dec', { _key: 'test' }, {alias: 'alias', controlled: true, token: ''} ) testProperty!: string; + @T( + 'not-trans-dec', + { _key: 'test' }, + { alias: 'alias', controlled: true, token: '' } + ) + testProperty!: string; } beforeEach(() => { @@ -33,7 +38,9 @@ describe('T Decorator', () => { fixture.detectChanges(); expect(tx.translate).toHaveBeenCalled(); - expect(tx.translate).toHaveBeenCalledWith('not-trans-dec', { _key: 'test' }); + expect(tx.translate).toHaveBeenCalledWith('not-trans-dec', { + _key: 'test', + }); expect(component.testProperty).toBe('ok-translated-dec'); const compiled: HTMLElement = fixture.debugElement.nativeElement; @@ -41,15 +48,20 @@ describe('T Decorator', () => { }); it('should test the decorator T with an instance', () => { - const fixtureWithInstance = TestBed.createComponent(TestWithInstanceComponent); + const fixtureWithInstance = TestBed.createComponent( + TestWithInstanceComponent + ); const component = fixtureWithInstance.componentInstance; fixtureWithInstance.detectChanges(); expect(tx.translate).toHaveBeenCalled(); - expect(tx.translate).toHaveBeenCalledWith('not-trans-dec', { _key: 'test' }); + expect(tx.translate).toHaveBeenCalledWith('not-trans-dec', { + _key: 'test', + }); expect(component.testProperty).toBe('ok-translated-dec'); - const compiled: HTMLElement = fixtureWithInstance.debugElement.nativeElement; + const compiled: HTMLElement = + fixtureWithInstance.debugElement.nativeElement; expect(compiled.innerHTML).toContain('ok-translated-dec'); }); }); diff --git a/packages/angular/projects/tx-native-angular-sdk/src/tests/TXNative.module.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/TXNative.module.spec.ts new file mode 100644 index 00000000..6367dbd9 --- /dev/null +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/TXNative.module.spec.ts @@ -0,0 +1,41 @@ +import { TestBed } from '@angular/core/testing'; +import { TranslationService, TxNativeModule } from '../public-api'; + +class CustomService { + testMethod(): string { + return 'test'; + } +} + +describe('TxNativeModule', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [TxNativeModule], + }); + }); + + it("should not provide 'CustomService' service", () => { + expect(() => TestBed.inject(CustomService)).toThrowError(/No provider for/); + }); +}); + +describe('TxNativeModule.forRoot()', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [TxNativeModule.forRoot()], + }); + }); + + it('should provide services', () => { + // assert + expect(TestBed.inject(TranslationService)).toBeTruthy(); + }); + + it("should provide a single instance for 'TranslationService'", () => { + const translationService: TranslationService = + TestBed.inject(TranslationService); + + // assert + expect(translationService).toBeDefined(); + }); +}); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/UT.component.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/UT.component.spec.ts similarity index 56% rename from packages/angular/projects/tx-native-angular-sdk/tests/UT.component.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/UT.component.spec.ts index c6389d95..798e32ff 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/UT.component.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/UT.component.spec.ts @@ -1,8 +1,12 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { of } from 'rxjs'; -import { UTComponent } from '../src/lib/UT.component'; -import { SafeHtmlPipe, TranslationService, TXInstanceComponent } from '../src/public-api'; +import { UTComponent } from '../lib/UT.component'; +import { + SafeHtmlPipe, + TranslationService, + TXInstanceComponent, +} from '../public-api'; describe('UTComponent', () => { let component: UTComponent; @@ -23,8 +27,7 @@ describe('UTComponent', () => { await TestBed.configureTestingModule({ declarations: [UTComponent, SafeHtmlPipe, TXInstanceComponent], providers: [TXInstanceComponent], - }) - .compileComponents(); + }).compileComponents(); service = TestBed.inject(TranslationService); spyOn(service, 'setCurrentLocale'); spyOn(service, 'getCurrentLocale').and.returnValue('en'); @@ -39,8 +42,11 @@ describe('UTComponent', () => { it('should create the component', () => { // setup spyOn(component, 'translate'); - const localeChangedSpy = spyOnProperty(component, 'localeChanged', 'get') - .and.returnValue(of('el')); + const localeChangedSpy = spyOnProperty( + component, + 'localeChanged', + 'get' + ).and.returnValue(of('el')); // act component.ngOnInit(); @@ -65,8 +71,11 @@ describe('UTComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams }, + '' + ); expect(component.translatedStr).toEqual('translated'); }); @@ -81,45 +90,54 @@ describe('UTComponent', () => { fixture.detectChanges(); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams, _key: 'key-not-translated' }, ''); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams, _key: 'key-not-translated' }, + '' + ); expect(component.translatedStr).toEqual('translated'); }); - it('should translate and sanitize the string using div as wrapper', - () => { - // setup - spyOn(service, 'translate').and.returnValue('translated'); - - // act - component.str = 'not-translated'; - component.ngOnInit(); - fixture.detectChanges(); - - // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams, _inline: false }, ''); - const compiled = fixture.debugElement.nativeElement; - expect((compiled as HTMLDivElement).innerHTML) - .toContain('
translated
'); - }); - - it('should translate and sanitize the string using span as wrapper', - () => { - // setup - spyOn(service, 'translate').and.returnValue('translated'); - - // act - component.str = 'not-translated'; - component.inline = true; - component.ngOnInit(); - fixture.detectChanges(); - - // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', - { ...translationParams, _inline: true }, ''); - const compiled = fixture.debugElement.nativeElement; - expect((compiled as HTMLDivElement).innerHTML) - .toContain('translated'); - }); + it('should translate and sanitize the string using div as wrapper', () => { + // setup + spyOn(service, 'translate').and.returnValue('translated'); + + // act + component.str = 'not-translated'; + component.ngOnInit(); + fixture.detectChanges(); + + // assert + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams, _inline: false }, + '' + ); + const compiled = fixture.debugElement.nativeElement; + expect((compiled as HTMLDivElement).innerHTML).toContain( + '
translated
' + ); + }); + + it('should translate and sanitize the string using span as wrapper', () => { + // setup + spyOn(service, 'translate').and.returnValue('translated'); + + // act + component.str = 'not-translated'; + component.inline = true; + component.ngOnInit(); + fixture.detectChanges(); + + // assert + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + { ...translationParams, _inline: true }, + '' + ); + const compiled = fixture.debugElement.nativeElement; + expect((compiled as HTMLDivElement).innerHTML).toContain( + 'translated' + ); + }); }); diff --git a/packages/angular/projects/tx-native-angular-sdk/src/tests/instance.component.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/instance.component.spec.ts new file mode 100644 index 00000000..c0acc367 --- /dev/null +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/instance.component.spec.ts @@ -0,0 +1,67 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { tx } from '@transifex/native'; + +import { TranslationService, TXInstanceComponent } from '../public-api'; + +describe('TXInstanceComponent', () => { + let component: TXInstanceComponent; + let fixture: ComponentFixture; + let service: TranslationService; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [TXInstanceComponent], + providers: [TXInstanceComponent], + }).compileComponents(); + + service = TestBed.inject(TranslationService); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(TXInstanceComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should create the component and the instance', async () => { + // setup + component.token = 'token'; + component.alias = 'alias'; + spyOn(service, 'addInstance').and.resolveTo(true); + spyOn(service, 'getInstance').and.returnValue(tx); + + // act + await component.ngOnInit(); + fixture.detectChanges(); + + // assert + expect(component).toBeTruthy(); + expect(service).toBeTruthy(); + expect(service.addInstance).toHaveBeenCalled(); + expect(service.getInstance).toHaveBeenCalled(); + component.instanceIsReady.subscribe((value) => expect(value).toBe(true)); + }); + + it('should create the component and fail to create the instance', async () => { + // setup + component.token = 'token'; + component.alias = 'alias'; + spyOn(service, 'addInstance').and.resolveTo(false); + spyOn(service, 'getInstance'); + + // act + await component.ngOnInit(); + fixture.detectChanges(); + + // assert + expect(component).toBeTruthy(); + expect(service).toBeTruthy(); + expect(service.addInstance).toHaveBeenCalled(); + expect(service.getInstance).toHaveBeenCalled(); + component.instanceIsReady.subscribe((value) => expect(value).toBe(false)); + }); +}); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/language-picker.component.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/language-picker.component.spec.ts similarity index 84% rename from packages/angular/projects/tx-native-angular-sdk/tests/language-picker.component.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/language-picker.component.spec.ts index b81c997f..e9c79b80 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/language-picker.component.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/language-picker.component.spec.ts @@ -1,9 +1,13 @@ -import {ComponentFixture, TestBed} from '@angular/core/testing'; -import {By} from '@angular/platform-browser'; -import {of} from 'rxjs'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { of } from 'rxjs'; -import {LanguagePickerComponent} from '../src/lib/language-picker.component'; -import {ILanguage, TranslationService, TXInstanceComponent} from '../src/public-api'; +import { LanguagePickerComponent } from '../lib/language-picker.component'; +import { + ILanguage, + TranslationService, + TXInstanceComponent, +} from '../public-api'; describe('LanguagePickerComponent', () => { let component: LanguagePickerComponent; @@ -20,8 +24,7 @@ describe('LanguagePickerComponent', () => { await TestBed.configureTestingModule({ declarations: [LanguagePickerComponent, TXInstanceComponent], providers: [TXInstanceComponent], - }) - .compileComponents(); + }).compileComponents(); service = TestBed.inject(TranslationService); instance = TestBed.inject(TXInstanceComponent); }); @@ -59,8 +62,7 @@ describe('LanguagePickerComponent', () => { // assert const compiled = fixture.debugElement.nativeElement; const selectOptions = (compiled as HTMLDivElement).querySelector('select'); - expect((compiled as HTMLDivElement).innerHTML) - .toContain(' { fixture.detectChanges(); // assert - const select: HTMLSelectElement = fixture.debugElement.query(By.css('.tx-language-picker')).nativeElement; + const select: HTMLSelectElement = fixture.debugElement.query( + By.css('.tx-language-picker') + ).nativeElement; const optValue = select.options[1]?.value; if (optValue === undefined) { throw new Error('select.options[1]?.value is undefined'); @@ -86,7 +90,9 @@ describe('LanguagePickerComponent', () => { const optLabel = select.options[select.selectedIndex]?.label; if (optLabel === undefined) { - throw new Error('select.options[select.selectedIndex]?.label is undefined'); + throw new Error( + 'select.options[select.selectedIndex]?.label is undefined' + ); } expect(optLabel).toBe('Ελληνικά'); expect(component.onChange).toHaveBeenCalled(); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/load-translations.directive.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/load-translations.directive.spec.ts similarity index 85% rename from packages/angular/projects/tx-native-angular-sdk/tests/load-translations.directive.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/load-translations.directive.spec.ts index dd1b6c21..9830de08 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/load-translations.directive.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/load-translations.directive.spec.ts @@ -1,16 +1,13 @@ -import {Component, DebugElement } from '@angular/core'; +import { Component, DebugElement } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { ReplaySubject } from 'rxjs'; -import { LoadTranslationsDirective, TranslationService } from '../src/public-api'; - +import { LoadTranslationsDirective, TranslationService } from '../public-api'; describe('LoadTranslationsDirective', () => { @Component({ - template: ` -
- `, + template: `
`, }) class TestComponent {} @@ -31,14 +28,15 @@ describe('LoadTranslationsDirective', () => { service = TestBed.inject(TranslationService); spyOn(service, 'getCurrentLocale').and.returnValue('en'); - spyOnProperty(service, 'localeChanged', 'get').and. - returnValue(localeChangedSubject); + spyOnProperty(service, 'localeChanged', 'get').and.returnValue( + localeChangedSubject + ); spyOn(service, 'setCurrentLocale').and.callFake(async (locale) => { localeChangedSubject.next(locale); }); directives = fixture.debugElement.queryAll( - By.directive(LoadTranslationsDirective), + By.directive(LoadTranslationsDirective) ); fixture.detectChanges(); }); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/translate.pipe.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/translate.pipe.spec.ts similarity index 83% rename from packages/angular/projects/tx-native-angular-sdk/tests/translate.pipe.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/translate.pipe.spec.ts index 0a3518aa..49163365 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/translate.pipe.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/translate.pipe.spec.ts @@ -2,9 +2,9 @@ import { ChangeDetectorRef } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { tx } from '@transifex/native'; import { ReplaySubject } from 'rxjs'; -import { TranslatePipe } from '../src/lib/translate.pipe'; -import { TranslationService } from '../src/lib/translation.service'; -import { TXInstanceComponent } from '../src/public-api'; +import { TranslatePipe } from '../lib/translate.pipe'; +import { TranslationService } from '../lib/translation.service'; +import { TXInstanceComponent } from '../public-api'; describe('TranslatePipe', () => { let localeChangedSubject: ReplaySubject; @@ -26,13 +26,16 @@ describe('TranslatePipe', () => { beforeEach(async () => { TestBed.configureTestingModule({ - declarations: [ TXInstanceComponent ], + declarations: [TXInstanceComponent], providers: [ TranslationService, TXInstanceComponent, { provide: ChangeDetectorRef, - useValue: jasmine.createSpyObj('ChangeDetectorRef', [ 'markForCheck' ]), + useValue: jasmine.createSpyObj( + 'ChangeDetectorRef', + ['markForCheck'] + ), }, ], }); @@ -45,7 +48,9 @@ describe('TranslatePipe', () => { localeChangedSubject = new ReplaySubject(0); spyOn(service, 'getCurrentLocale').and.returnValue('en'); - spyOnProperty(service, 'localeChanged', 'get').and.returnValue(localeChangedSubject); + spyOnProperty(service, 'localeChanged', 'get').and.returnValue( + localeChangedSubject + ); spyOn(service, 'setCurrentLocale').and.callFake(async (locale) => { localeChangedSubject.next(locale); }); @@ -91,13 +96,16 @@ describe('TranslatePipe', () => { spyOn(service, 'translate').and.returnValue('translated'); // act - const translatedStr = translatePipe.transform('not-translated', translationParams); + const translatedStr = translatePipe.transform( + 'not-translated', + translationParams + ); // assert expect(service.translate).toHaveBeenCalledWith( - 'not-translated', - { ...translationParams }, - '', + 'not-translated', + { ...translationParams }, + '' ); expect(translatedStr).toEqual('translated'); }); @@ -113,7 +121,11 @@ describe('TranslatePipe', () => { const translatedStr = translatePipe.transform('not-translated'); // assert - expect(service.translate).toHaveBeenCalledWith('not-translated', {}, 'alias'); + expect(service.translate).toHaveBeenCalledWith( + 'not-translated', + {}, + 'alias' + ); expect(translatedStr).toEqual('translated'); }); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/translation.service.spec.ts b/packages/angular/projects/tx-native-angular-sdk/src/tests/translation.service.spec.ts similarity index 87% rename from packages/angular/projects/tx-native-angular-sdk/tests/translation.service.spec.ts rename to packages/angular/projects/tx-native-angular-sdk/src/tests/translation.service.spec.ts index e25e45a2..3a8e70e9 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tests/translation.service.spec.ts +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/translation.service.spec.ts @@ -1,8 +1,8 @@ import { TestBed } from '@angular/core/testing'; -import { tx, ILanguage, ITranslationConfig, TxNative } from '@transifex/native'; +import { ILanguage, ITranslationConfig, TxNative, tx } from '@transifex/native'; -import { TranslationService } from '../src/lib/translation.service'; -import { ITranslationServiceConfig } from '../src/public-api'; +import { TranslationService } from '../lib/translation.service'; +import { ITranslationServiceConfig } from '../public-api'; describe('TranslationService', () => { let service: TranslationService; @@ -66,11 +66,15 @@ describe('TranslationService', () => { spyOn(tx, 'translate').and.returnValue('translated'); // act - const result = service.translate('not-translated', { ...translationParams }); + const result = service.translate('not-translated', { + ...translationParams, + }); // assert expect(result).toBe('translated'); - expect(tx.translate).toHaveBeenCalledWith('not-translated', { ...translationParams }); + expect(tx.translate).toHaveBeenCalledWith('not-translated', { + ...translationParams, + }); }); it('should translate with key', () => { @@ -78,11 +82,17 @@ describe('TranslationService', () => { spyOn(tx, 'translate').and.returnValue('translated'); // act - const result = service.translate('not-translated', { ...translationParams, _key: 'key-string' }); + const result = service.translate('not-translated', { + ...translationParams, + _key: 'key-string', + }); // assert expect(result).toBe('translated'); - expect(tx.translate).toHaveBeenCalledWith('not-translated', { ...translationParams, _key: 'key-string' }); + expect(tx.translate).toHaveBeenCalledWith('not-translated', { + ...translationParams, + _key: 'key-string', + }); }); it('should translate and escape', () => { @@ -90,11 +100,17 @@ describe('TranslationService', () => { spyOn(tx, 'translate').and.returnValue('Hola {username}'); // act - const result = service.translate('Hello {username}', { ...translationParams, _escapeVars: true }); + const result = service.translate('Hello {username}', { + ...translationParams, + _escapeVars: true, + }); // assert expect(result).toBe('Hola {username}'); - expect(tx.translate).toHaveBeenCalledWith('Hello {username}', { ...translationParams, _escapeVars: true }); + expect(tx.translate).toHaveBeenCalledWith('Hello {username}', { + ...translationParams, + _escapeVars: true, + }); }); it('should set current locale', async () => { @@ -179,7 +195,9 @@ describe('TranslationService', () => { }; // act/assert - await expectAsync(service.addInstance(instanceConfig)).toBeResolvedTo(false); + await expectAsync(service.addInstance(instanceConfig)).toBeResolvedTo( + false + ); expect(tx.controllerOf).not.toHaveBeenCalled(); }); @@ -194,7 +212,9 @@ describe('TranslationService', () => { }; // act/assert - await expectAsync(service.addInstance(instanceConfig)).toBeResolvedTo(false); + await expectAsync(service.addInstance(instanceConfig)).toBeResolvedTo( + false + ); expect(tx.controllerOf).not.toHaveBeenCalled(); }); @@ -247,7 +267,9 @@ describe('TranslationService', () => { await service.fetchTranslations('tag1'); // assert - expect(tx.fetchTranslations).toHaveBeenCalledWith('en', { filterTags: 'tag1' }); + expect(tx.fetchTranslations).toHaveBeenCalledWith('en', { + filterTags: 'tag1', + }); }); it('should fetch translations on demand without custom instance no fetched tags', async () => { @@ -263,7 +285,9 @@ describe('TranslationService', () => { await service.fetchTranslations('tag1'); // assert - expect(tx.fetchTranslations).toHaveBeenCalledWith('en', { filterTags: 'tag1' }); + expect(tx.fetchTranslations).toHaveBeenCalledWith('en', { + filterTags: 'tag1', + }); }); it('should fetch translations on demand with custom instance', async () => { @@ -280,7 +304,9 @@ describe('TranslationService', () => { // assert expect(service.getInstance).toHaveBeenCalledWith('my-instance'); - expect(tx.fetchTranslations).toHaveBeenCalledWith('en', { filterTags: 'tag1' }); + expect(tx.fetchTranslations).toHaveBeenCalledWith('en', { + filterTags: 'tag1', + }); }); it('should not fetch translations on demand if no instance', async () => { diff --git a/packages/angular/projects/tx-native-angular-sdk/src/tests/tsconfig.json b/packages/angular/projects/tx-native-angular-sdk/src/tests/tsconfig.json new file mode 100644 index 00000000..540c1c6c --- /dev/null +++ b/packages/angular/projects/tx-native-angular-sdk/src/tests/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.spec.json", + "compilerOptions": { + "types": ["jasmine"], + "typeRoots": ["../../../../node_modules/@types"] + }, + "include": ["**/*.spec.ts"] +} diff --git a/packages/angular/projects/tx-native-angular-sdk/test.ts b/packages/angular/projects/tx-native-angular-sdk/test.ts index 695bd56e..4a1d5154 100644 --- a/packages/angular/projects/tx-native-angular-sdk/test.ts +++ b/packages/angular/projects/tx-native-angular-sdk/test.ts @@ -1,27 +1,17 @@ // This file is required by karma.conf.js and loads recursively all the .spec and framework files - -import 'zone.js'; import 'zone.js/testing'; -import { getTestBed } from '@angular/core/testing'; -import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from "@angular/platform-browser-dynamic/testing"; - - -declare const require: { - context(path: string, deep?: boolean, filter?: RegExp): { - keys(): string[]; - (id: string): T; - }; -}; +import { getTestBed } from '@angular/core/testing'; +import { + BrowserDynamicTestingModule, + platformBrowserDynamicTesting, +} from '@angular/platform-browser-dynamic/testing'; // First, initialize the Angular testing environment. getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting(), { - teardown: { destroyAfterEach: true } - }, + BrowserDynamicTestingModule, + platformBrowserDynamicTesting(), + { + teardown: { destroyAfterEach: true }, + } ); -// Then we find all the tests. -const context = require.context('./tests/', true, /\.spec\.ts$/); -// And load the modules. -context.keys().map(context); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/TXNative.module.spec.ts b/packages/angular/projects/tx-native-angular-sdk/tests/TXNative.module.spec.ts deleted file mode 100644 index ae1493d4..00000000 --- a/packages/angular/projects/tx-native-angular-sdk/tests/TXNative.module.spec.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { TranslationService, TxNativeModule } from '../src/public-api'; - -class CustomService { - testMethod(): string { - return 'test'; - } -} - -describe('TxNativeModule', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [TxNativeModule], - }); - }); - - it('should not provide \'CustomService\' service', () => { - expect( - () => TestBed.inject(CustomService), - ).toThrowError(/No provider for/); - }); -}); - -describe('TxNativeModule.forRoot()', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [ - TxNativeModule.forRoot(), - ], - }); - }); - - it('should provide services', () => { - // assert - expect(TestBed.inject(TranslationService)).toBeTruthy(); - }); - - it('should provide a single instance for \'TranslationService\'', - () => { - const translationService: TranslationService = TestBed.inject( - TranslationService, - ); - - // assert - expect(translationService).toBeDefined(); - }); -}); diff --git a/packages/angular/projects/tx-native-angular-sdk/tests/instance.component.spec.ts b/packages/angular/projects/tx-native-angular-sdk/tests/instance.component.spec.ts deleted file mode 100644 index 31574e41..00000000 --- a/packages/angular/projects/tx-native-angular-sdk/tests/instance.component.spec.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { tx } from '@transifex/native'; - -import { TranslationService, TXInstanceComponent } from '../src/public-api'; - -describe('TXInstanceComponent', () => { - let component: TXInstanceComponent; - let fixture: ComponentFixture; - let service: TranslationService; - - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [ TXInstanceComponent ], - providers: [ TXInstanceComponent ], - }) - .compileComponents(); - - service = TestBed.inject(TranslationService); - }); - - beforeEach(() => { - fixture = TestBed.createComponent(TXInstanceComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); - - it('should create the component and the instance', async () => { - // setup - component.token = 'token'; - component.alias = 'alias'; - spyOn(service, 'addInstance').and.resolveTo(true); - spyOn(service, 'getInstance').and.returnValue(tx); - - // act - await component.ngOnInit(); - fixture.detectChanges(); - - // assert - expect(component).toBeTruthy(); - expect(service).toBeTruthy(); - expect(service.addInstance).toHaveBeenCalled(); - expect(service.getInstance).toHaveBeenCalled(); - component.instanceIsReady.subscribe((value) => expect(value).toBe(true)); - }); - - it('should create the component and fail to create the instance', async () => { - // setup - component.token = 'token'; - component.alias = 'alias'; - spyOn(service, 'addInstance').and.resolveTo(false); - spyOn(service, 'getInstance'); - - // act - await component.ngOnInit(); - fixture.detectChanges(); - - // assert - expect(component).toBeTruthy(); - expect(service).toBeTruthy(); - expect(service.addInstance).toHaveBeenCalled(); - expect(service.getInstance).toHaveBeenCalled(); - component.instanceIsReady.subscribe((value) => expect(value).toBe(false)); - }); -}); diff --git a/packages/angular/projects/tx-native-angular-sdk/tsconfig.lib.json b/packages/angular/projects/tx-native-angular-sdk/tsconfig.lib.json index 68c6392d..143048d1 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tsconfig.lib.json +++ b/packages/angular/projects/tx-native-angular-sdk/tsconfig.lib.json @@ -3,7 +3,6 @@ "extends": "../../tsconfig.json", "compilerOptions": { "outDir": "../../out-tsc/lib", - "target": "es2020", "declaration": true, "declarationMap": true, "inlineSources": true, diff --git a/packages/angular/projects/tx-native-angular-sdk/tsconfig.spec.json b/packages/angular/projects/tx-native-angular-sdk/tsconfig.spec.json index e2ecda63..7fb03743 100644 --- a/packages/angular/projects/tx-native-angular-sdk/tsconfig.spec.json +++ b/packages/angular/projects/tx-native-angular-sdk/tsconfig.spec.json @@ -3,17 +3,8 @@ "extends": "../../tsconfig.json", "compilerOptions": { "outDir": "../../out-tsc/spec", - "types": [ - "jasmine", - "node" - ], - "typeRoots": [ "../node_modules/@types" ], + "types": ["jasmine"], + "typeRoots": ["../../node_modules/@types"] }, - "files": [ - "test.ts" - ], - "include": [ - "**/*.spec.ts", - "**/*.d.ts" - ] + "include": ["**/*.spec.ts", "**/*.d.ts"] } diff --git a/packages/angular/tsconfig.json b/packages/angular/tsconfig.json index 6e892de1..09fc3f6d 100644 --- a/packages/angular/tsconfig.json +++ b/packages/angular/tsconfig.json @@ -19,20 +19,26 @@ "experimentalDecorators": true, "moduleResolution": "node", "importHelpers": true, - "target": "es2015", + "target": "ES2022", "module": "es2020", "lib": [ "es2020", "dom" ], - "types": [ "node", "jasmine" ], - "typeRoots": [ "../node_modules/@types" ], + "types": [ + "node", + "jasmine" + ], + "typeRoots": [ + "../node_modules/@types" + ], "paths": { "tx-native-angular-sdk": [ "dist/tx-native-angular-sdk/tx-native-angular-sdk", "dist/tx-native-angular-sdk" ] - } + }, + "useDefineForClassFields": false }, "angularCompilerOptions": { "enableI18nLegacyMessageIdFormat": false,