Skip to content
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

Unit test: inject KnoraApiConnection and spy on lib endpoint methods #145

Open
flavens opened this issue Feb 6, 2020 · 1 comment
Open
Assignees
Labels
testing Testing of components of this lib
Milestone

Comments

@flavens
Copy link
Contributor

flavens commented Feb 6, 2020

We found a way to inject an instance of knora-api-connection and spy on one method in the knora-api-js-lib. However, this is not ideal and the MockFactory should provide a simpler way to get test data and call endpoint methods (e.g. getProjects()).

In this example, we create the spy in the beforeEach() because the method to get projects in the component is triggered in ngOnInit().

main.component.ts

 ngOnInit() {
       [...]
        this.loadProjects();
}

loadProjects() {
        this.loading = true;
        this.knoraApiConnection.admin.projectsEndpoint.getProjects().subscribe(
            (response: ApiResponseData<ProjectsResponse>) => { ... }
);}

main.component.spec.ts

[...]
beforeEach(inject([KnoraApiConnectionToken], (knoraApiConn) => {
        projectSpy = spyOn(knoraApiConn.admin.projectsEndpoint, 'getProjects').and.callFake(
            () => {
                const projectList = {
                    body: {
                        projects: [
                            {
                                id: 'http://rdfh.ch/projects/0801',
                                description: [
                                    {
                                        value: 'Bernoulli-Euler Online'
                                    }
                                ],
                                keywords: [],
                                logo: null,
                                longname: 'Bernoulli-Euler Online',
                                ontologies: [
                                    'http://www.knora.org/ontology/0801/leibniz',
                                    'http://www.knora.org/ontology/0801/biblio',
                                    'http://www.knora.org/ontology/0801/newton',
                                    'http://www.knora.org/ontology/0801/beol'
                                ],
                                selfjoin: false,
                                shortcode: '0801',
                                shortname: 'beol',
                                status: true
                            },
                            {
                                id: 'http://rdfh.ch/projects/0001',
                                description: [
                                    {
                                        value: 'Anything Project'
                                    }
                                ],
                                keywords: [],
                                logo: null,
                                longname: 'Anything Project',
                                ontologies: [
                                    'http://www.knora.org/ontology/0001/anything',
                                    'http://www.knora.org/ontology/0001/minimal',
                                    'http://www.knora.org/ontology/0001/something'
                                ],
                                selfjoin: false,
                                shortcode: '0001',
                                shortname: 'anything',
                                status: true
                            }
                        ]
                    }
                };

                return of(projectList);
            });

        fixture = TestBed.createComponent(MainComponent);
        component = fixture.componentInstance;
        element = fixture.nativeElement; // the HTML reference
        fixture.detectChanges();
}));

it('should load projects', () => {
        expect(projectSpy).toHaveBeenCalledTimes(1);
});
@flavens flavens added the testing Testing of components of this lib label Feb 6, 2020
@subotic subotic added this to the Backlog milestone Feb 7, 2020
@tobiasschweizer
Copy link
Contributor

I guess it should not be hard to add something to the public API that generates admin response data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Testing of components of this lib
Projects
None yet
Development

No branches or pull requests

3 participants