Add StringCollection to make it easier to work with strings
This commit is contained in:
		| @@ -15,20 +15,20 @@ describe('DirectiveParser', () => { | ||||
|  | ||||
| 	it('should extract contents when no translate attribute value is provided', () => { | ||||
| 		const contents = '<div translate>Hello World</div>'; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract translate attribute if provided', () => { | ||||
| 		const contents = '<div translate="KEY">Hello World<div>'; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['KEY']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['KEY']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract bound translate attribute as key if provided', () => { | ||||
| 		const contents = `<div [translate]="'KEY'">Hello World<div>`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['KEY']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['KEY']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract direct text nodes when no translate attribute value is provided', () => { | ||||
| @@ -39,8 +39,8 @@ describe('DirectiveParser', () => { | ||||
| 				Hi <em>there</em> | ||||
| 			</div> | ||||
| 		`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello', 'Hi']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello', 'Hi']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract direct text nodes of tags with a translate attribute', () => { | ||||
| @@ -51,8 +51,8 @@ describe('DirectiveParser', () => { | ||||
| 				<div translate>Hi there</div> | ||||
| 			</div> | ||||
| 		`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World', 'Hi there']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World', 'Hi there']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract translate attribute if provided or direct text nodes if not', () => { | ||||
| @@ -64,8 +64,8 @@ describe('DirectiveParser', () => { | ||||
| 				<p [translate]="'OTHER_KEY'">Lorem Ipsum</p> | ||||
| 			</div> | ||||
| 		`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['KEY', 'Hi there', 'OTHER_KEY']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['KEY', 'Hi there', 'OTHER_KEY']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract and parse inline template', () => { | ||||
| @@ -76,26 +76,26 @@ describe('DirectiveParser', () => { | ||||
| 			}) | ||||
| 			export class TestComponent { } | ||||
| 		`; | ||||
| 		const messages = parser.process(componentFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, componentFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract contents when no ng2-translate attribute value is provided', () => { | ||||
| 		const contents = '<div ng2-translate>Hello World</div>'; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract ng2-translate attribute if provided', () => { | ||||
| 		const contents = '<div ng2-translate="KEY">Hello World<div>'; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['KEY']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['KEY']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract bound ng2-translate attribute as key if provided', () => { | ||||
| 		const contents = `<div [ng2-translate]="'KEY'">Hello World<div>`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['KEY']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['KEY']); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
|   | ||||
| @@ -14,20 +14,20 @@ describe('PipeParser', () => { | ||||
|  | ||||
| 	it('should extract interpolated strings using translate pipe', () => { | ||||
| 		const contents = `Hello {{ 'World' | translate }}`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['World']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract interpolated strings using translate pipe in attributes', () => { | ||||
| 		const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract bound strings using translate pipe in attributes', () => { | ||||
| 		const contents = `<span [attr]="'Hello World' | translate"></span>`; | ||||
| 		const messages = parser.process(templateFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, templateFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
|   | ||||
| @@ -30,8 +30,8 @@ describe('ServiceParser', () => { | ||||
| 					protected _translateService: TranslateService | ||||
| 			) { } | ||||
| 		`; | ||||
| 		const messages = parser.extractTranslateServiceVar(contents); | ||||
| 		expect(messages).to.equal('_translateService'); | ||||
| 		const name = parser.extractTranslateServiceVar(contents); | ||||
| 		expect(name).to.equal('_translateService'); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract strings in TranslateService\'s get() method', () => { | ||||
| @@ -43,8 +43,8 @@ describe('ServiceParser', () => { | ||||
| 					this._translateService.get('Hello World'); | ||||
| 				} | ||||
| 		`; | ||||
| 		const messages = parser.process(componentFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, componentFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract strings in TranslateService\'s instant() method', () => { | ||||
| @@ -56,8 +56,8 @@ describe('ServiceParser', () => { | ||||
| 					this._translateService.instant('Hello World'); | ||||
| 				} | ||||
| 		`; | ||||
| 		const messages = parser.process(componentFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello World']); | ||||
| 		const keys = parser.extract(contents, componentFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract array of strings in TranslateService\'s get() method', () => { | ||||
| @@ -69,8 +69,8 @@ describe('ServiceParser', () => { | ||||
| 					this._translateService.get(['Hello', 'World']); | ||||
| 				} | ||||
| 		`; | ||||
| 		const messages = parser.process(componentFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello', 'World']); | ||||
| 		const keys = parser.extract(contents, componentFilename).keys(); | ||||
| 		expect(keys).to.deep.equal(['Hello', 'World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should extract array of strings in TranslateService\'s instant() method', () => { | ||||
| @@ -82,8 +82,8 @@ describe('ServiceParser', () => { | ||||
| 					this._translateService.instant(['Hello', 'World']); | ||||
| 				} | ||||
| 		`; | ||||
| 		const messages = parser.process(componentFilename, contents); | ||||
| 		expect(messages).to.deep.equal(['Hello', 'World']); | ||||
| 		const key = parser.extract(contents, componentFilename).keys(); | ||||
| 		expect(key).to.deep.equal(['Hello', 'World']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should not extract strings in get()/instant() methods of other services', () => { | ||||
| @@ -99,8 +99,8 @@ describe('ServiceParser', () => { | ||||
| 					this._otherService.instant('Hi there'); | ||||
| 				} | ||||
| 		`; | ||||
| 		const messages = parser.process(componentFilename, contents); | ||||
| 		expect(messages).to.deep.equal([]); | ||||
| 		const keys = parser.extract(contents, componentFilename).keys(); | ||||
| 		expect(keys).to.deep.equal([]); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
|   | ||||
							
								
								
									
										54
									
								
								tests/utils/string.collection.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								tests/utils/string.collection.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| import { expect } from 'chai'; | ||||
|  | ||||
| import { StringCollection } from '../../src/utils/string.collection'; | ||||
|  | ||||
| describe('StringCollection', () => { | ||||
|  | ||||
| 	let collection: StringCollection; | ||||
|  | ||||
| 	beforeEach(() => { | ||||
| 		collection = new StringCollection(); | ||||
| 	}); | ||||
|  | ||||
| 	it('should add item with value', () => { | ||||
| 		collection.add('key', 'translation'); | ||||
| 		expect(collection.get('key')).to.equal('translation'); | ||||
| 	}); | ||||
|  | ||||
| 	it('should add item with default value', () => { | ||||
| 		collection.add('key'); | ||||
| 		expect(collection.get('key')).to.equal(''); | ||||
| 	}); | ||||
|  | ||||
| 	it('should add array of items with default values', () => { | ||||
| 		collection.add(['key', 'key2']); | ||||
| 		expect(collection.count()).to.equal(2); | ||||
| 	}); | ||||
|  | ||||
| 	it('should remove item', () => { | ||||
| 		collection.add('key1').add('key2').remove('key1'); | ||||
| 		expect(collection.count()).to.equal(1); | ||||
| 	}); | ||||
|  | ||||
| 	it('should return number of items', () => { | ||||
| 		collection.add('key1').add('key2'); | ||||
| 		expect(collection.count()).to.equal(2); | ||||
| 	}); | ||||
|  | ||||
| 	it('should initialize with array of keys', () => { | ||||
| 		const newCollection = StringCollection.fromArray(['Hello', 'World']); | ||||
| 		expect(newCollection.count()).to.equal(2); | ||||
| 	}); | ||||
|  | ||||
| 	it('should initialize with key/value pairs', () => { | ||||
| 		const newCollection = StringCollection.fromObject({'key': 'translation'}); | ||||
| 		expect(newCollection.get('key')).to.equal('translation'); | ||||
| 	}); | ||||
|  | ||||
| 	it('should merge with other collection', () => { | ||||
| 		collection.add('Hello'); | ||||
| 		const newCollection = StringCollection.fromArray(['World']); | ||||
| 		expect(collection.merge(newCollection).count()).to.equal(2); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user