dasch-swiss / beol

Bernoulli-Euler OnLine
https://beol.dasch.swiss
GNU Affero General Public License v3.0
0 stars 1 forks source link

Use settings from AppInitService #122

Closed tobiasschweizer closed 5 years ago

tobiasschweizer commented 5 years ago

Use AppInitService instead of environment.

tobiasschweizer commented 5 years ago

@subotic I do not know how to mock AppInitService in the specs. The members of AppInitService are static and so I cannot just inject a mocked instance.

If AppInitService was an argument in the constructor of an Angular component (an injected service), it could be mocked as follows:

TestBed.configureTestingModule({
            imports: [
                ...
            ],
            declarations: [...],
            providers: [
                { provide: AppInitService, useValue: myMockedInstance }
            ]
tobiasschweizer commented 5 years ago

I think it could work like this:

@Component({
    selector: 'app-leoo-route',
    templateUrl: './bebb-route.component.html',
    styleUrls: ['./leoo-route.component.scss']
})
export class BebbRouteComponent implements OnInit {

    bebbLettertitle: string;
    notFound: boolean;

    constructor(
        private _route: ActivatedRoute,
        private _router: Router,
        private _beolService: BeolService,
        private _searchService: SearchService,
        private _appInitService: AppInitService) {
    }

    ngOnInit() {...}

and then in the spec:

providers: [
                { provide: AppInitService, useValue: { settings: { ontologyIRI: 'test'} } }
            ]
tobiasschweizer commented 5 years ago

and then the members would not be static:

@Injectable()
export class AppInitService {

    settings: IAppConfig;
    coreConfig: KuiCoreConfig;

    constructor() {
    }

    Init() {

        return new Promise<void>((resolve, reject) => {
            // console.log('AppInitService.init() called');
            // do your initialisation stuff here

            const data = <IAppConfig> window['tempConfigStorage'];
            // console.log('AppInitService: json', data);
            this.settings = data;

            this.coreConfig = <KuiCoreConfig> {
                name: this.settings.appName,
                api: this.settings.apiURL,
                media: this.settings.iiifURL,
                app: this.settings.appURL
            };

            // console.log('AppInitService: finished');

            resolve();
        });
    }
}