-
-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue 1163] Add additional tests #1169
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,28 +72,21 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy { | |
if (forceUpdate) { | ||
node.lastKey = null; | ||
} | ||
if(isDefined(node.lookupKey)) { | ||
key = node.lookupKey; | ||
} else if (this.key) { | ||
if (this.key) { | ||
key = this.key; | ||
} else { | ||
let content = this.getContent(node); | ||
let trimmedContent = content.trim(); | ||
if (trimmedContent.length) { | ||
node.lookupKey = trimmedContent; | ||
// we want to use the content as a key, not the translation value | ||
if (content !== node.currentValue) { | ||
key = trimmedContent; | ||
// the content was changed from the user, we'll use it as a reference if needed | ||
node.originalContent = content || node.originalContent; | ||
} else if (node.originalContent) { // the content seems ok, but the lang has changed | ||
if (node.originalContent && forceUpdate) { // the content seems ok, but the lang has changed | ||
node.lastKey = null; | ||
// the current content is the translation, not the key, use the last real content as key | ||
key = node.originalContent.trim(); | ||
} else if (content !== node.currentValue) { | ||
// we want to use the content as a key, not the translation value | ||
key = trimmedContent; | ||
// the content was changed from the user, we'll use it as a reference if needed | ||
node.originalContent = content || node.originalContent; | ||
node.originalContent = content; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import {ChangeDetectionStrategy, Component, ElementRef, Injectable, ViewChild, ViewContainerRef} from '@angular/core'; | ||
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Injectable, ViewChild} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {TranslateModule, TranslateService} from '../src/public_api'; | ||
|
||
|
@@ -17,12 +17,14 @@ import {TranslateModule, TranslateService} from '../src/public_api'; | |
<div #leadingSpaceNoKeyNoParams translate> TEST</div> | ||
<div #trailingSpaceNoKeyNoParams translate>TEST </div> | ||
<div #withSpaceAndLineBreakNoKeyNoParams translate> | ||
TEST | ||
NESTED.TEST.KEY | ||
</div> | ||
<div #interpolatedWithSpaceAndLineBreakNoKeyNoParams translate> | ||
{{ myInterpolatedTranslationKey }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding tests for interpolated bound template variables. |
||
</div> | ||
` | ||
}) | ||
class App { | ||
viewContainerRef: ViewContainerRef; | ||
@ViewChild('noKey', {static: true}) noKey: ElementRef; | ||
@ViewChild('contentAsKey', {static: true}) contentAsKey: ElementRef; | ||
@ViewChild('withKey', {static: true}) withKey: ElementRef; | ||
|
@@ -33,12 +35,12 @@ class App { | |
@ViewChild('leadingSpaceNoKeyNoParams') leadingSpaceNoKeyNoParams: ElementRef; | ||
@ViewChild('trailingSpaceNoKeyNoParams') trailingSpaceNoKeyNoParams: ElementRef; | ||
@ViewChild('withSpaceAndLineBreakNoKeyNoParams') withSpaceAndLineBreakNoKeyNoParams: ElementRef; | ||
@ViewChild('interpolatedWithSpaceAndLineBreakNoKeyNoParams') interpolatedWithSpaceAndLineBreakNoKeyNoParams: ElementRef; | ||
value = {value: 'ok'}; | ||
myInterpolatedTranslationKey = 'Placeholder Text'; | ||
|
||
constructor(viewContainerRef: ViewContainerRef) { | ||
this.viewContainerRef = viewContainerRef; | ||
constructor(public changeDetectorRef: ChangeDetectorRef) { } | ||
} | ||
} | ||
|
||
describe('TranslateDirective', () => { | ||
let translate: TranslateService; | ||
|
@@ -51,7 +53,7 @@ describe('TranslateDirective', () => { | |
], | ||
declarations: [App] | ||
}); | ||
translate = TestBed.inject(TranslateService); | ||
translate = TestBed.get(TranslateService); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw that this had been recently changed, but the tests work either way, so I'm unsure why it changed? If it needs to be |
||
|
||
fixture = (<any>TestBed).createComponent(App); | ||
fixture.detectChanges(); | ||
|
@@ -164,19 +166,53 @@ describe('TranslateDirective', () => { | |
}); | ||
|
||
it('should update the DOM when the lang changes and the translation key has line breaks and spaces', () => { | ||
expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(' TEST '); | ||
expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(' NESTED.TEST.KEY '); | ||
|
||
const en = "This is a test - with trailing spaces in translation key"; | ||
const fr = "C'est un test - avec un espace de fuite dans la clé de traduction"; | ||
const whiteSpaceFromKey = ' '; | ||
translate.setTranslation('en', {"TEST": en}); | ||
translate.setTranslation('fr', {"TEST": fr}); | ||
translate.setTranslation("en", { "NESTED": { "TEST": { "KEY": en } } }); | ||
translate.setTranslation("fr", { "NESTED": { "TEST": { "KEY": fr } } }); | ||
|
||
translate.use('en'); | ||
expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(whiteSpaceFromKey + en + whiteSpaceFromKey); | ||
expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` ${en} `); | ||
|
||
translate.use('fr'); | ||
expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(whiteSpaceFromKey + fr + whiteSpaceFromKey); | ||
expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` ${fr} `); | ||
}); | ||
|
||
it('should update the DOM when the lang changes and the translation key is interpolated', async () => { | ||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(' Placeholder Text '); | ||
|
||
const en1="This is a test - with trailing spaces in interpolated translation key"; | ||
const en2="Another test"; | ||
const fr1="C'est un test - pardon, je ne parle pas francais :)"; | ||
const fr2="Un autre test"; | ||
const setKey = key => { | ||
fixture.componentInstance.myInterpolatedTranslationKey = key; | ||
fixture.componentInstance.changeDetectorRef.markForCheck(); // Needed when changeDetection is ChangeDetectionStrategy.OnPush | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why the test harness is configured with |
||
fixture.detectChanges(); | ||
}; | ||
|
||
translate.setTranslation("en", { "NESTED": { "TEST3": en1, "TEST4": en2 } }); | ||
translate.setTranslation("fr", { "NESTED": { "TEST3": fr1, "TEST4": fr2 } }); | ||
translate.use('en'); | ||
|
||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(' Placeholder Text '); | ||
|
||
setKey('NESTED.TEST3'); | ||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` ${en1} `); | ||
|
||
setKey('NESTED.TEST4'); | ||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` ${en2} `); | ||
|
||
translate.use('fr'); | ||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` ${fr2} `); | ||
|
||
setKey('NESTED.TEST3'); | ||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` ${fr1} `); | ||
|
||
setKey(''); // display nothing | ||
expect(fixture.componentInstance.interpolatedWithSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(` `); | ||
}); | ||
|
||
it('should update the DOM when the lang changes and the translation key ends with space', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that the lookup key will prevent the translation of an updated interpolated bound template variable.