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

fix(react): build & tests #73

Merged
merged 1 commit into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core/src/entities/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventKey, EventListener, EventListenerOptions, EventSource, EventUnsubscribe } from '../events';
import { AegisQuery, QueryManager, QueryManagerEventMap, QueryStatus, RefreshStrategy } from '../protocols';
import { AegisQuery, QueryManager, QueryManagerEventMap, RefreshStrategy } from '../protocols';
import { PartialKey, StringKey } from '../utils';

import { AegisEntity } from './entity';
Expand Down Expand Up @@ -94,8 +94,8 @@ export class AegisList<D> extends EventSource<ListEventMap<D>> {
return this._manager.query;
}

get status(): QueryStatus {
return this.query?.status ?? 'pending';
get isLoading(): boolean {
return this.query?.status === 'pending';
}

get data(): D[] {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/tests/entities/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ describe('AegisList.refresh', () => {
});
});

describe('AegisList.status', () => {
describe('AegisList.isLoading', () => {
it('should return pending if no query is running', () => {
expect(list.status).toBe('pending');
expect(list.isLoading).toBe(false);
});

it('should return query status', () => {
Expand All @@ -93,19 +93,19 @@ describe('AegisList.status', () => {
jest.spyOn(query, 'status', 'get')
.mockReturnValue('pending');

expect(list.status).toBe('pending');
expect(list.isLoading).toBe(true);

// - completed
jest.spyOn(query, 'status', 'get')
.mockReturnValue('completed');

expect(list.status).toBe('completed');
expect(list.isLoading).toBe(false);

// - error
jest.spyOn(query, 'status', 'get')
.mockReturnValue('failed');

expect(list.status).toBe('failed');
expect(list.isLoading).toBe(false);
});
});

8 changes: 4 additions & 4 deletions packages/react/src/hook.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export function $hook<T, E extends Aegis<T, unknown>>(entity: E) {
const id = args[0].id;
const item = useMemo(() => entity.$entity.item(id), [id]);

const { status, data } = useAegisItem(item);
const { isLoading, data } = useAegisItem(item);

useEffect(() => {
query(..._args);
}, [_args]);

return {
item,
status, data,
isLoading, data,
refresh: useCallback(() => query(..._args), [_args]),
};
};
Expand All @@ -53,15 +53,15 @@ export function $hook<T, E extends Aegis<T, unknown>>(entity: E) {
const _args = useDeepMemo(args);
const list = useMemo(() => entity.$entity.list(key), []);

const { status, data } = useAegisList(list);
const { isLoading, data } = useAegisList(list);

useEffect(() => {
query(key, ..._args);
}, [_args]);

return {
list,
status, data,
isLoading, data,
refresh: useCallback(() => query(key, ..._args), [_args]),
};
};
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/hooks/useAegisItem.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { AegisItem, QueryStatus } from '@jujulego/aegis-core';
import { AegisItem } from '@jujulego/aegis-core';
import { useDebugValue } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';

// Types
export interface AegisItemState<T> {
status: QueryStatus;
isLoading: boolean;
data?: T;
}

// Hooks
export function useAegisItem<T>(item: AegisItem<T>): AegisItemState<T> {
const status = useSyncExternalStore((cb) => item.subscribe('query', cb), () => item.status);
const isLoading = useSyncExternalStore((cb) => item.subscribe('query', cb), () => item.isLoading);
const data = useSyncExternalStore((cb) => item.subscribe('update', cb), () => item.data);

useDebugValue(data);

return {
status,
isLoading,
data,
};
}
8 changes: 4 additions & 4 deletions packages/react/src/hooks/useAegisList.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { AegisList, QueryStatus } from '@jujulego/aegis-core';
import { AegisList } from '@jujulego/aegis-core';
import { useDebugValue } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';

// Types
export interface AegisListState<T> {
status: QueryStatus;
isLoading: boolean;
data: T[];
}

// Hooks
export function useAegisList<T>(list: AegisList<T>): AegisListState<T> {
const status = useSyncExternalStore((cb) => list.subscribe('query', cb), () => list.status);
const isLoading = useSyncExternalStore((cb) => list.subscribe('query', cb), () => list.isLoading);
const data = useSyncExternalStore((cb) => list.subscribe('update', cb), () => list.data);

useDebugValue(data);

return {
status,
isLoading,
data
};
}
14 changes: 7 additions & 7 deletions packages/react/tests/hook.builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('$hook().item', () => {

expect(fetch).toHaveBeenCalledWith({ id: 'test' });
expect(result.current).toEqual({
status: 'pending',
isLoading: true,
item: expect.any(AegisItem),
refresh: expect.any(Function),
});
Expand All @@ -36,7 +36,7 @@ describe('$hook().item', () => {
});

expect(result.current).toEqual({
status: 'completed',
isLoading: false,
data: { id: 'test', success: true },
item: expect.any(AegisItem),
refresh: expect.any(Function),
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('$hook().item', () => {

expect(fetch).toHaveBeenCalledTimes(2);
expect(result.current).toEqual({
status: 'pending',
isLoading: true,
data: { id: 'test', success: true },
item: expect.any(AegisItem),
refresh: expect.any(Function),
Expand All @@ -90,7 +90,7 @@ describe('$hook().item', () => {

expect(fetch).toHaveBeenCalledWith({ id: 'test-1' });
expect(result.current).toEqual({
status: 'pending',
isLoading: true,
item: expect.any(AegisItem),
refresh: expect.any(Function),
});
Expand All @@ -100,7 +100,7 @@ describe('$hook().item', () => {

expect(fetch).toHaveBeenCalledWith({ id: 'test-2' });
expect(result.current).toEqual({
status: 'pending',
isLoading: true,
item: expect.any(AegisItem),
refresh: expect.any(Function),
});
Expand All @@ -123,7 +123,7 @@ describe('$hook().list', () => {

expect(fetch).toHaveBeenCalledWith(6, false);
expect(result.current).toEqual({
status: 'pending',
isLoading: true,
data: [],
list: expect.any(AegisList),
refresh: expect.any(Function),
Expand All @@ -138,7 +138,7 @@ describe('$hook().list', () => {
});

expect(result.current).toEqual({
status: 'completed',
isLoading: false,
data: [
{ id: 'test-1', success: true },
{ id: 'test-2', success: true },
Expand Down
4 changes: 2 additions & 2 deletions packages/react/tests/hooks/useAegisItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('useAegisItem', () => {
const { result } = renderHook(() => useAegisItem(itm));

expect(result.current).toEqual({
status: 'pending',
isLoading: false,
data: {
id: 'test-1',
success: true
Expand All @@ -40,7 +40,7 @@ describe('useAegisItem', () => {
});

expect(result.current).toEqual({
status: 'pending',
isLoading: false,
data: {
id: 'test-1',
success: false
Expand Down
4 changes: 2 additions & 2 deletions packages/react/tests/hooks/useAegisList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('useAegisList', () => {
const { result } = renderHook(() => useAegisList(lst));

expect(result.current).toEqual({
status: 'pending',
isLoading: false,
data: [
{ id: 'test-1', success: true },
{ id: 'test-2', success: true }
Expand All @@ -40,7 +40,7 @@ describe('useAegisList', () => {
});

expect(result.current).toEqual({
status: 'pending',
isLoading: false,
data: [
{ id: 'test-1', success: false },
{ id: 'test-3', success: true }
Expand Down