tflori / angular-translator

translation module for angular
https://tflori.github.io/angular-translator/
MIT License
21 stars 6 forks source link

Provide a FakeTranslationLoader #50

Closed tflori closed 7 years ago

tflori commented 7 years ago

Now that I'm testing my app using I find out it would be helpful to have a fake translation loader that just returns an empty object.

With this loader we can guarantee that the translation is the provided key.

Its very easy too:

export class TranslationLoaderFake extends TranslationLoader {
  public load(): Promise<object> {
    return Promise.resolve({});
  }
}

We could also move the flattenTranslations method to the abstract class TranslationLoader and make it protected. This way the TranslationLoaderFake can use this method too what gives a nice difference too:

export class TranslationLoaderFake extends TranslationLoader {
  protected translations: any;

  constructor(translations: any = {}) {
    this.translations = this.flattenTranslations(translations);
    super();
  }

  public load(): Promise<object> {
    return Promise.resolve(this.translations);
  }
}

beforeEach(async(() => {
  TestBed.ConfigureTestingModule({
    imports: [
      TranslatorModule.forRoot({ loader: TranslationLoaderFake });
    ],
    providers: [
      { provide: TranslationLoaderFake, useValue: new FakeTranslationLoader(require('../assets/i18n/en.json')) },
    ],
  });
}));