-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
component.unit.ts
47 lines (43 loc) · 1.59 KB
/
component.unit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @license
* Copyright Builder.io, Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/BuilderIO/qwik/blob/main/LICENSE
*/
import { ComponentFixture } from '@builder.io/qwik/testing';
import { GreeterComponent } from '../util/test_component_fixture';
import { AttributeMarker } from '../util/markers';
import { Component } from './component';
import { injectMethod } from '../injector/inject';
describe('component', () => {
it('should declare a component', async () => {
const fixture = new ComponentFixture();
fixture.host.setAttribute(
AttributeMarker.ComponentTemplate,
String(GreeterComponent.$templateQRL)
);
fixture.host.setAttribute('salutation', 'Hello');
fixture.host.setAttribute('name', 'World');
const greeter = await fixture.injector.getComponent(GreeterComponent);
expect(greeter.$props).toEqual({ salutation: 'Hello', name: 'World' });
expect(greeter.$state).toEqual({ greeting: 'Hello World!' });
});
it('should call $init state', () => {
const fixture = new ComponentFixture();
class MyComponent extends Component<'props', 'state'> {
async $newState(): Promise<'state'> {
return 'state';
}
}
const myComponent = new MyComponent(fixture.host, 'props', 'state');
expect(myComponent).toEqual({
$state: 'state',
$props: 'props',
$host: fixture.host,
});
});
});
export const greet = injectMethod(GreeterComponent, function (this: GreeterComponent) {
return this.$state.greeting;
});