-
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 all 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" | ||
] | ||
} |
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 |
---|---|---|
|
@@ -59,3 +59,6 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# dist | ||
dist/ |
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
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,39 @@ | ||
import * as React from 'react'; | ||
import { render, cleanup, waitForElement } from 'react-testing-library'; | ||
import 'jest-dom/extend-expect' | ||
import { PluggedTestComponent } from '../lib/utils/example-plug'; | ||
import { setTotal10ChangedTrue } from '../lib/utils/example-store'; | ||
|
||
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,74 @@ | ||
import { take, skip } from 'rxjs/operators'; | ||
import { state$, setTotal10ChangedTrue, setTotal20, getCurrentState, setTotalDouble } from '../lib/utils/example-store'; | ||
|
||
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
module.exports = { | ||
roots: [ | ||
"<rootDir>" | ||
], | ||
globals: { | ||
'ts-jest': { | ||
babelConfig: true | ||
} | ||
}, | ||
moduleFileExtensions: [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json" | ||
], | ||
} |
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; | ||
} |
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,51 @@ | ||
import * as React from 'react'; | ||
import { Component } from 'react'; | ||
import { Subscription } from 'rxjs'; | ||
import { TStream, ILifecycle, IPlugState } from './interfaces/plug'; | ||
|
||
|
||
export const plug = <T extends {}>(stream: TStream, lifecycleHooks: Partial<ILifecycle> = {}, | ||
) => (WrappedComponent: any) => | ||
class PluggedComponent extends Component<T, IPlugState> { | ||
|
||
_streamSubscription!: Subscription; | ||
|
||
constructor(props: any) { | ||
super(props); | ||
|
||
this.state = { | ||
hasEmmited: false, | ||
}; | ||
} | ||
|
||
public componentDidMount() { | ||
this._streamSubscription = stream(this.props).subscribe((data: any) => | ||
this.setState({ | ||
hasEmmited: true, | ||
innerData: data, | ||
}), | ||
); | ||
|
||
if (lifecycleHooks.componentDidMount) { | ||
lifecycleHooks.componentDidMount(); | ||
} | ||
} | ||
|
||
public componentWillUnmount() { | ||
this._streamSubscription.unsubscribe(); | ||
|
||
if (lifecycleHooks.componentWillUnmount) { | ||
lifecycleHooks.componentWillUnmount(); | ||
} | ||
} | ||
|
||
public 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,34 @@ | ||
import { IStore } from './interfaces/store'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { clone } from './utils/clone'; | ||
|
||
function createStore<IState>(initialState: IState): IStore<IState> { | ||
const behavior$ = new BehaviorSubject<IState>(clone(initialState)); | ||
const updateState = (request: any) => { | ||
const isFunction = request instanceof Function; | ||
const currentState = clone(behavior$.getValue()); | ||
const newKeys = isFunction ? request(currentState) : request; | ||
|
||
behavior$.next({ | ||
...(currentState as any), | ||
...newKeys | ||
}); | ||
}; | ||
|
||
function stateInjector<T>(func: Function): () => T { | ||
return (): T => func(behavior$.getValue()); | ||
} | ||
|
||
const getCurrentState = (): any => behavior$.getValue(); | ||
|
||
const state$ = behavior$.asObservable(); | ||
|
||
return { | ||
state$, | ||
stateInjector, | ||
updateState, | ||
getCurrentState, | ||
}; | ||
} | ||
|
||
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 @@ | ||
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as React from 'react'; | ||
import { plug } from "../../lib"; | ||
import { map, combineLatest } from "rxjs/operators"; | ||
import { of, Observable } from "rxjs"; | ||
import { IStore, state$ } from './example-store'; | ||
|
||
interface IProp { | ||
name: string; | ||
} | ||
|
||
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 PluggedTestComponent = plug<IProp>(stream)(TestComponent); | ||
|
||
export { | ||
PluggedTestComponent, | ||
} |
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 @@ | ||
import { createStore } from "../../lib"; | ||
|
||
export interface IStore { | ||
total: number; | ||
changed: boolean; | ||
} | ||
|
||
const initialState: IStore = { | ||
total: 0, | ||
changed: false | ||
} | ||
|
||
const { updateState, getCurrentState, state$ } = createStore(initialState); | ||
|
||
const setTotal10ChangedTrue = () => updateState({ | ||
total: 10, changed: true | ||
}); | ||
|
||
const setTotal20 = () => updateState({ | ||
total: 20 | ||
}); | ||
|
||
const setTotalDouble = () => updateState((state: any) => ({ | ||
total: state.total * 2 | ||
})); | ||
|
||
export { | ||
state$, | ||
getCurrentState, | ||
setTotal10ChangedTrue, | ||
setTotal20, | ||
setTotalDouble | ||
} |
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.
futuramente podemos usar Hooks aqui