-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare const clone: (target: any) => any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export declare function testLib(): string; | ||
import { plug } from "./plug"; | ||
import { createStore } from "./store"; | ||
export { plug, createStore }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remover extensão desnecessária |
||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const clone = (target: any) => JSON.parse(JSON.stringify(target)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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