Skip to content

Commit

Permalink
refactor: convert unit tests to jest and testing submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 17, 2021
1 parent b1cb1d7 commit c6ce111
Show file tree
Hide file tree
Showing 39 changed files with 595 additions and 614 deletions.
7 changes: 0 additions & 7 deletions src/core/assert/assert.unit.ts

This file was deleted.

16 changes: 8 additions & 8 deletions src/core/component/component.unit.ts
Expand Up @@ -6,11 +6,11 @@
* found in the LICENSE file at https://github.com/BuilderIO/qwik/blob/main/LICENSE
*/

import { expect } from 'chai';
import { ComponentFixture, GreeterComponent } from '../testing/component_fixture.js';
import { AttributeMarker } from '../util/markers.js';
import { Component } from './component.js';
import { injectMethod } from '../injector/inject.js';
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 () => {
Expand All @@ -22,8 +22,8 @@ describe('component', () => {
fixture.host.setAttribute('salutation', 'Hello');
fixture.host.setAttribute('name', 'World');
const greeter = await fixture.injector.getComponent(GreeterComponent);
expect(greeter.$props).to.eql({ salutation: 'Hello', name: 'World' });
expect(greeter.$state).to.eql({ greeting: 'Hello World!' });
expect(greeter.$props).toEqual({ salutation: 'Hello', name: 'World' });
expect(greeter.$state).toEqual({ greeting: 'Hello World!' });
});

it('should call $init state', () => {
Expand All @@ -34,7 +34,7 @@ describe('component', () => {
}
}
const myComponent = new MyComponent(fixture.host, 'props', 'state');
expect(myComponent).to.eql({
expect(myComponent).toEqual({
$state: 'state',
$props: 'props',
$host: fixture.host,
Expand Down
90 changes: 44 additions & 46 deletions src/core/entity/entity.unit.ts
Expand Up @@ -6,17 +6,15 @@
* found in the LICENSE file at https://github.com/BuilderIO/qwik/blob/main/LICENSE
*/

import { expect } from 'chai';
import { assertDefined } from '../assert/assert.js';
import { TEST_CONFIG } from '../testing/config.unit.js';
import { stringifyDebug } from '../error/stringify.js';
import { QRL } from '../import/qrl.js';
import { getInjector } from '../injector/element_injector.js';
import { injectMethod } from '../injector/inject.js';
import { serializeState } from '../render/serialize_state.js';
import { ElementFixture } from '../testing/element_fixture.js';
import { Entity, EntityConstructor } from './entity.js';
import type { EntityKey } from './entity_key.js';
import { assertDefined } from '../assert/assert';
import { TEST_CONFIG } from '../util/test_config';
import { stringifyDebug } from '../error/stringify';
import { QRL } from '../import/qrl';
import { getInjector } from '../injector/element_injector';
import { injectMethod } from '../injector/inject';
import { ElementFixture, serializeState } from '@builder.io/qwik/testing';
import { Entity, EntityConstructor } from './entity';
import type { EntityKey } from './entity_key';

export const __verify_Entity_subtype_of_EntityType__: EntityConstructor<any> = Entity;
const entity: Entity<any, any> = null!;
Expand All @@ -37,7 +35,7 @@ describe('entity', () => {
it('should attach', () => {
const fixture = new ElementFixture(TEST_CONFIG);
GreeterEntity.$attachEntity(fixture.host);
expect(stringifyDebug(fixture.host)).to.eql(
expect(stringifyDebug(fixture.host)).toEqual(
`<host ::greeter='entity:/entity.unit#GreeterEntity'>`
);
});
Expand All @@ -46,26 +44,26 @@ describe('entity', () => {
const fixture = new ElementFixture(TEST_CONFIG);
GreeterEntity.$attachEntity(fixture.host);
GreeterEntity.$attachEntity(fixture.host);
expect(stringifyDebug(fixture.host)).to.eql(
expect(stringifyDebug(fixture.host)).toEqual(
`<host ::greeter='entity:/entity.unit#GreeterEntity'>`
);
});

it('should error', () => {
expect(() => Entity.$attachEntity.apply(MissingNameEntity as any, null!)).to.throw(
expect(() => Entity.$attachEntity.apply(MissingNameEntity as any, null!)).toThrow(
`SERVICE-ERROR(Q-310): Entity 'MissingNameEntity' must have static '$type' property defining the name of the entity.`
);
expect(() => Entity.$attachEntity.apply(EmptyNameEntity as any, null!)).to.throw(
expect(() => Entity.$attachEntity.apply(EmptyNameEntity as any, null!)).toThrow(
`SERVICE-ERROR(Q-310): Entity 'EmptyNameEntity' must have static '$type' property defining the name of the entity.`
);
expect(() => Entity.$attachEntity.apply(MissingKeyPropsEntity as any, null!)).to.throw(
expect(() => Entity.$attachEntity.apply(MissingKeyPropsEntity as any, null!)).toThrow(
`SERVICE-ERROR(Q-312): Entity 'MissingKeyPropsEntity' must have static '$qrl' property defining the import location of the entity.`
);
});
it('should error on QRL collision', () => {
const fixture = new ElementFixture(TEST_CONFIG);
fixture.host.setAttribute('::greeter', 'some_other_qrl');
expect(() => GreeterEntity.$attachEntity(fixture.host)).to.throw(
expect(() => GreeterEntity.$attachEntity(fixture.host)).toThrow(
`SERVICE-ERROR(Q-313): Name collision. Already have entity named 'Greeter' with QRL 'some_other_qrl' but expected QRL 'entity:/entity.unit#GreeterEntity'.`
);
});
Expand All @@ -84,7 +82,7 @@ describe('entity', () => {
{ salutation: 'ahoj', name: 'svet' },
{ greeting: 'Ahoj Svet!' }
);
expect(stringifyDebug(fixture.host)).to.eql(
expect(stringifyDebug(fixture.host)).toEqual(
`<host ::greeter='entity:/entity.unit#GreeterEntity' ` +
`greeter:ahoj:svet='{"greeting":"Ahoj Svet!"}' ` +
`greeter:hello:world='{"greeting":"Hello World!"}'>`
Expand All @@ -100,10 +98,10 @@ describe('entity', () => {
{ salutation: 'Hello', name: 'World' },
{ greeting: 'existing state' }
);
expect(greeterPromise.$key).to.eql('greeter:-hello:-world');
expect(greeterPromise.$key).toEqual('greeter:-hello:-world');
const greeter = await greeterPromise;
expect(greeter.$state).to.eql({ $key: 'greeter:-hello:-world', greeting: 'existing state' });
expect(stringifyDebug(fixture.host)).to.eql(
expect(greeter.$state).toEqual({ $key: 'greeter:-hello:-world', greeting: 'existing state' });
expect(stringifyDebug(fixture.host)).toEqual(
`<host : ::greeter='entity:/entity.unit#GreeterEntity' greeter:-hello:-world>`
);
});
Expand All @@ -113,13 +111,13 @@ describe('entity', () => {
salutation: 'Hello',
name: 'World',
});
expect(greeterPromise.$key).to.eql('greeter:-hello:-world');
expect(greeterPromise.$key).toEqual('greeter:-hello:-world');
const greeter = await greeterPromise;
expect(greeter.$state).to.eql({
expect(greeter.$state).toEqual({
$key: 'greeter:-hello:-world',
greeting: 'INIT: Hello World!',
});
expect(stringifyDebug(fixture.host)).to.eql(
expect(stringifyDebug(fixture.host)).toEqual(
`<host : ::greeter='entity:/entity.unit#GreeterEntity' greeter:-hello:-world>`
);
});
Expand All @@ -129,21 +127,21 @@ describe('entity', () => {
salutation: 'Hello',
name: 'World',
});
expect(greeterPromise).to.equal(GreeterEntity.$hydrate(fixture.host, greeterHelloWorldKey));
expect(await greeterPromise).to.equal(
expect(greeterPromise).toEqual(GreeterEntity.$hydrate(fixture.host, greeterHelloWorldKey));
expect(await greeterPromise).toEqual(
await GreeterEntity.$hydrate(fixture.host, greeterHelloWorldKey)
);
});
it('should hydrate without state using key', async () => {
const fixture = new ElementFixture(TEST_CONFIG);
const greeterPromise = GreeterEntity.$hydrate(fixture.host, greeterHelloWorldKey);
expect(greeterPromise.$key).to.eql('greeter:-hello:-world');
expect(greeterPromise.$key).toEqual('greeter:-hello:-world');
const greeter = await greeterPromise;
expect(greeter.$state).to.eql({
expect(greeter.$state).toEqual({
$key: 'greeter:-hello:-world',
greeting: 'INIT: Hello World!',
});
expect(stringifyDebug(fixture.host)).to.eql(
expect(stringifyDebug(fixture.host)).toEqual(
`<host : ::greeter='entity:/entity.unit#GreeterEntity' greeter:-hello:-world>`
);
});
Expand All @@ -153,12 +151,12 @@ describe('entity', () => {
salutation: 'throw',
name: 'World',
});
expect(greeterPromise.$key).to.eql('greeter:throw:-world');
expect(greeterPromise.$key).toEqual('greeter:throw:-world');
try {
await greeterPromise;
expect('not to get here').to.be.false;
expect('not to get here').toBeFalsy();
} catch (e) {
expect(String(e)).to.contain('Error: World');
expect(String(e)).toContain('Error: World');
}
});
it('should deserialize entity from DOM', async () => {
Expand All @@ -172,25 +170,25 @@ describe('entity', () => {
const injector = getInjector(fixture.child);
const greeterPromise = injector.getEntityState(greeterAhojSvetKey);
const greeter: Greeter = await greeterPromise;
expect(greeter).to.eql({ $key: 'greeter:ahoj:svet', greeting: 'Ahoj Svet!' });
expect(greeter).toEqual({ $key: 'greeter:ahoj:svet', greeting: 'Ahoj Svet!' });

const entityPromise = getInjector(fixture.child).getEntity<GreeterEntity>(greeterAhojSvetKey);
const greeterEntity = await entityPromise;
expect(greeterEntity).to.be.instanceOf(GreeterEntity);
expect(greeterEntity.$props).to.eql({ salutation: 'ahoj', name: 'svet' });
expect(greeterEntity.$state).to.eql({ $key: 'greeter:ahoj:svet', greeting: 'Ahoj Svet!' });
expect(greeterEntity).toBeInstanceOf(GreeterEntity);
expect(greeterEntity.$props).toEqual({ salutation: 'ahoj', name: 'svet' });
expect(greeterEntity.$state).toEqual({ $key: 'greeter:ahoj:svet', greeting: 'Ahoj Svet!' });
});
});

describe('invoke', () => {
it('should create an instance and invoke identity method', async () => {
const fixture = new ElementFixture(TEST_CONFIG);
const empty = await EmptyEntity.$hydrate(fixture.child, {}, {});
expect(await empty.ident('ABC')).to.equal('ABC');
expect(await getInjector(fixture.child).getEntity('empty:' as any as EntityKey)).to.equal(
expect(await empty.ident('ABC')).toEqual('ABC');
expect(await getInjector(fixture.child).getEntity('empty:' as any as EntityKey)).toEqual(
empty
);
expect(fixture.child.getAttribute('::empty')).to.equal('entity:/entity.unit#EmptyEntity');
expect(fixture.child.getAttribute('::empty')).toEqual('entity:/entity.unit#EmptyEntity');
});
});

Expand All @@ -201,25 +199,25 @@ describe('entity', () => {
name: 'world',
});
const greeter = await greeterPromise;
expect(greeter.$state.greeting).to.equal('INIT: hello world!');
expect(await greeter.greet()).to.equal('hello world!');
expect(greeter.$state.greeting).to.equal('hello world!');
expect(stringifyDebug(greeter.$element)).to.equal(
expect(greeter.$state.greeting).toEqual('INIT: hello world!');
expect(await greeter.greet()).toEqual('hello world!');
expect(greeter.$state.greeting).toEqual('hello world!');
expect(stringifyDebug(greeter.$element)).toEqual(
"<child : ::greeter='entity:/entity.unit#GreeterEntity' greeter:hello:world>"
);
expect(
await getInjector(fixture.child).getEntity(
'greeter:hello:world' as any as EntityKey<GreeterEntity>
)
).to.equal(greeter);
).toEqual(greeter);

serializeState(fixture.host);
expect(stringifyDebug(greeter.$element)).to.equal(
expect(stringifyDebug(greeter.$element)).toEqual(
`<child : ::greeter='entity:/entity.unit#GreeterEntity' greeter:hello:world='{"greeting":"hello world!"}'>`
);

greeter.$release();
expect(stringifyDebug(greeter.$element)).to.equal(
expect(stringifyDebug(greeter.$element)).toEqual(
`<child : ::greeter='entity:/entity.unit#GreeterEntity'>`
);
});
Expand Down

0 comments on commit c6ce111

Please sign in to comment.