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

Feature/initial draft #3

Merged
merged 5 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
["@babel/preset-env",
{
"targets": {
"node": "current"
}
}],
"@babel/preset-react",
"@babel/preset-typescript"
]
}
Empty file added .watchmanconfig
Empty file.
41 changes: 41 additions & 0 deletions __tests__/plug.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react';
import { render, cleanup, waitForElement } from 'react-testing-library';
import 'jest-dom/extend-expect'
import { plug } from '../lib';
import { of, Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { PluggedTestComponent, setTotal10ChangedTrue } from '../lib/aux/aux-code';

describe('Plug', () => {
afterEach(cleanup);

it('Renders', async done => {
const { getByTestId } = render(<PluggedTestComponent name="Test Testersson" />);

const nameNode = await waitForElement(() => getByTestId('name'))
const totalNode = await waitForElement(() => getByTestId('total'))
const changedNode = await waitForElement(() => getByTestId('changed'))

expect(nameNode).toHaveTextContent('Test Testersson');
expect(totalNode).toHaveTextContent('Total: 0');
expect(changedNode).toHaveTextContent(`The store wasn't changed`);
done();
})

it('Renders with new State', async done => {
setTotal10ChangedTrue();

const { getByTestId } = render(<PluggedTestComponent name="Test Testersson" />);

const nameNode = await waitForElement(() => getByTestId('name'))
const totalNode = await waitForElement(() => getByTestId('total'))
const changedNode = await waitForElement(() => getByTestId('changed'))

expect(nameNode).toHaveTextContent('Test Testersson');
expect(totalNode).toHaveTextContent('Total: 10');
expect(changedNode).toHaveTextContent(`The store was changed`);
done();
})
})


75 changes: 75 additions & 0 deletions __tests__/store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { createStore } from "../lib";
import { take, skip } from 'rxjs/operators';
import { state$, setTotal10ChangedTrue, setTotal20, getCurrentState, setTotalDouble } from "../lib/aux/aux-code";

describe('Store', () => {
beforeAll(() => {
process.env = Object.assign(process.env, { isTesting: 'true' });
});

it('Creates a Store', async done => {
state$.pipe(
take(1)
).subscribe(data => {
expect(data).toEqual({
total: 0,
changed: false
});
done();
})
});

it('Update State', async done => {
state$.pipe(
skip(1),
take(1)
).subscribe(data => {
expect(data).toEqual({
total: 10,
changed: true
});
done();
});

setTotal10ChangedTrue();
});

it('Update State Partially', async done => {
state$.pipe(
skip(1),
take(1)
).subscribe(data => {
expect(data).toEqual({
total: 20,
changed: true
});
done();
})

setTotal20();
});

it('Retrieve current State', () => {
const currentState = getCurrentState();

expect(currentState).toEqual({
total: 20,
changed: true
});
});

it('Update State using Callback', async done => {
state$.pipe(
skip(1),
take(1)
).subscribe(data => {
expect(data).toEqual({
total: 40,
changed: true
});
done();
})

setTotalDouble();
});
})
1 change: 1 addition & 0 deletions dist/aux/clone.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const clone: (target: any) => any;
3 changes: 3 additions & 0 deletions dist/aux/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clone = (target) => JSON.parse(JSON.stringify(target));
4 changes: 3 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export declare function testLib(): string;
import { plug } from "./plug";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tentar não usar a pasta dist e fazer isso no build

import { createStore } from "./store";
export { plug, createStore };
8 changes: 4 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function testLib() {
return "It's working!";
}
exports.testLib = testLib;
const plug_1 = require("./plug");
exports.plug = plug_1.plug;
const store_1 = require("./store");
exports.createStore = store_1.createStore;
10 changes: 10 additions & 0 deletions dist/interfaces/plug.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Observable } from "rxjs";
export declare type TStream = (stream: any) => Observable<any>;
export interface ILifecycle {
componentDidMount: () => void;
componentWillUnmount: () => void;
}
export interface IPlugState {
hasEmmited: boolean;
innerData?: any;
}
2 changes: 2 additions & 0 deletions dist/interfaces/plug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
7 changes: 7 additions & 0 deletions dist/interfaces/store.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BehaviorSubject } from 'rxjs';
export interface IStore<T> {
state$: BehaviorSubject<T>;
updateState(request: Function | Partial<T>): void;
stateInjector<T>(func: Function): () => T;
getCurrentState(): T;
}
2 changes: 2 additions & 0 deletions dist/interfaces/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
22 changes: 22 additions & 0 deletions dist/plug.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference types="react" />
import { Subscription } from 'rxjs';
import { TStream, ILifecycle, IPlugState } from './interfaces/plug';
export declare const plug: (stream: TStream, lifecycleHooks: Partial<ILifecycle>) => (WrappedComponent: any) => {
new (props: any): {
_streamSubscription: Subscription;
componentDidMount(): void;
componentWillUnmount(): void;
render(): JSX.Element | null;
context: any;
setState<K extends "innerData" | "hasEmmited">(state: IPlugState | ((prevState: Readonly<IPlugState>, props: Readonly<any>) => IPlugState | Pick<IPlugState, K> | null) | Pick<IPlugState, K> | null, callback?: (() => void) | undefined): void;
forceUpdate(callBack?: (() => void) | undefined): void;
readonly props: Readonly<any> & Readonly<{
children?: import("react").ReactNode;
}>;
state: Readonly<IPlugState>;
refs: {
[key: string]: import("react").ReactInstance;
};
};
contextType?: import("react").Context<any> | undefined;
};
33 changes: 33 additions & 0 deletions dist/plug.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
exports.plug = (stream, lifecycleHooks) => (WrappedComponent) => class PluggedComponent extends react_1.Component {
constructor(props) {
super(props);
this.state = {
hasEmmited: false,
};
}
componentDidMount() {
this._streamSubscription = stream(this.props).subscribe((data) => this.setState({
hasEmmited: true,
innerData: data,
}));
if (lifecycleHooks.componentDidMount) {
lifecycleHooks.componentDidMount();
}
}
componentWillUnmount() {
this._streamSubscription.unsubscribe();
if (lifecycleHooks.componentWillUnmount) {
lifecycleHooks.componentWillUnmount();
}
}
render() {
const { hasEmmited, innerData } = this.state;
if (hasEmmited) {
return <WrappedComponent {...innerData}/>;
}
return null;
}
};
3 changes: 3 additions & 0 deletions dist/store.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { IStore } from './interfaces/store';
declare function createStore<IState>(initialState: IState): IStore<IState>;
export { createStore };
24 changes: 24 additions & 0 deletions dist/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const clone_1 = require("./aux/clone");
const rxjs_1 = require("rxjs");
function createStore(initialState) {
const state$ = new rxjs_1.BehaviorSubject(clone_1.clone(initialState));
const updateState = (request) => {
const isFunction = request instanceof Function;
const currentState = clone_1.clone(state$.getValue());
const newKeys = isFunction ? request(currentState) : request;
state$.next(Object.assign({}, currentState, newKeys));
};
function stateInjector(func) {
return () => func(state$.getValue());
}
const getCurrentState = () => state$.getValue();
return {
state$,
stateInjector,
updateState,
getCurrentState,
};
}
exports.createStore = createStore;
18 changes: 18 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
roots: [
"<rootDir>"
],
globals: {
'ts-jest': {
babelConfig: true
}
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remover extensão desnecessária

],
}
67 changes: 67 additions & 0 deletions lib/aux/aux-code.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renomear aux para example

import { plug, createStore } from "../../lib";
import { map, combineLatest } from "rxjs/operators";
import { of, Observable } from "rxjs";

interface IProp {
name: string;
}

interface IStore {
total: number;
changed: boolean;
}

const initialState: IStore = {
total: 0,
changed: false
}

const { updateState, getCurrentState, state$ } = createStore(initialState);

const TestComponent = (props: IStore & IProp) => {
const { total, changed, name } = props;
return (
<div>
<h1 data-testid="name">{name}</h1>
<p data-testid="total">Total: {total}</p>
<p data-testid="changed">The store was{changed ? '' : `n't`} changed.</p>
</div>
)
}

const stream = (props: IProp): Observable<IStore & IProp> => {
return of(props).pipe(
combineLatest(state$),
map(([props, state]) => {
const data: IStore & IProp = {
name: props.name,
...state
}
return data;
})
);
}

const setTotal10ChangedTrue = () => updateState({
total: 10, changed: true
});

const setTotal20 = () => updateState({
total: 20
});

const setTotalDouble = () => updateState((state: any) => ({
total: state.total * 2
}));

const PluggedTestComponent = plug<IProp>(stream)(TestComponent);

export {
state$,
getCurrentState,
PluggedTestComponent,
setTotal10ChangedTrue,
setTotal20,
setTotalDouble
}
1 change: 1 addition & 0 deletions lib/aux/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const clone = (target: any) => JSON.parse(JSON.stringify(target));
8 changes: 6 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function testLib(): string {
return `It's working!`;
import { plug } from "./plug";
import { createStore } from "./store";

export {
plug,
createStore
}
13 changes: 13 additions & 0 deletions lib/interfaces/plug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Observable } from "rxjs";

export type TStream = (stream: any) => Observable<any>;

export interface ILifecycle {
componentDidMount: () => void;
componentWillUnmount: () => void;
}

export interface IPlugState {
hasEmmited: boolean;
innerData?: any;
}
8 changes: 8 additions & 0 deletions lib/interfaces/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Observable } from 'rxjs';

export interface IStore<T> {
state$: Observable<T>;
updateState(request: Function | Partial<T>): void;
stateInjector<T>(func: Function): () => T;
getCurrentState(): T;
}
Loading