MOBILE-3320 tests: Add link directive tests

main
Noel De Martin 2020-10-26 16:52:26 +01:00
parent 8cd764551f
commit c81b55820c
2 changed files with 57 additions and 11 deletions

View File

@ -0,0 +1,38 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreLinkDirective } from '@directives/link';
import { renderTemplate } from '@/tests/utils';
describe('CoreLinkDirective', () => {
it('should render', async () => {
// Act
const fixture = await renderTemplate(
CoreLinkDirective,
'<a href="https://moodle.org/" core-link [capture]="true">Link</a>',
);
// Assert
expect(fixture.nativeElement.innerHTML.trim()).not.toHaveLength(0);
const anchor = fixture.nativeElement.querySelector('a');
expect(anchor).not.toBeNull();
expect(anchor.href).toEqual('https://moodle.org/');
});
it.todo('should capture clicks');
});

View File

@ -73,6 +73,24 @@ export async function renderComponent<T>(component: Type<T>, config: Partial<Ren
});
}
export async function renderTemplate<T>(
component: Type<T>,
template: string,
config: Partial<RenderConfig> = {},
): Promise<WrapperComponentFixture<T>> {
config.declarations = config.declarations ?? [];
config.declarations.push(component);
return renderAngularComponent(
createWrapperComponent(template, component),
{
declarations: [],
providers: [],
...config,
},
);
}
export async function renderWrapperComponent<T>(
component: Type<T>,
tag: string,
@ -84,17 +102,7 @@ export async function renderWrapperComponent<T>(
.map(([name, value]) => `${name}="${value.toString().replace(/"/g, '&quot;')}"`)
.join(' ');
config.declarations = config.declarations ?? [];
config.declarations.push(component);
return renderAngularComponent(
createWrapperComponent(`<${tag} ${inputAttributes}></${tag}>`, component),
{
declarations: [],
providers: [],
...config,
},
);
return renderTemplate(component, `<${tag} ${inputAttributes}></${tag}>`, config);
}
async function renderAngularComponent<T>(component: Type<T>, config: RenderConfig): Promise<ComponentFixture<T>> {