Skip to content

Commit 52c75b8

Browse files
1) move index.test.js into src folder
2) make useDynamicTabs.js testable
1 parent 5ce1a59 commit 52c75b8

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
import useDynamicTabs from './useDynamicTabs/useDynamicTabs.js';
2+
import useDynamicTabs from './useDynamicTabs/index.js';
33
export default useDynamicTabs;

src/useDynamicTabs/index.test.js renamed to src/index.test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useState } from "react"
22
import { render, unmountComponentAtNode } from "react-dom";
3-
import useDynTabs from "./useDynamicTabs.js";
3+
import useDynTabs from "./index.js";
44
import { act } from "react-dom/test-utils";
5-
import renderer from 'react-test-renderer';
65
let container = document.createElement("div");
76
let op = '';//options
87
beforeAll(() => {
@@ -64,6 +63,31 @@ describe("actions", () => {
6463
expect(op.onSelect.mock.calls.length).toBe(1);
6564
expect(op.onLoad.mock.calls.length).toBe(1);
6665
});
66+
test('result of select and close actions', () => {
67+
expect.assertions(2);
68+
let _api;
69+
act(() => {
70+
const App = function (props) {
71+
const [Tablist, Panellist, api] = useDynTabs(op);
72+
_api = api;
73+
return (
74+
<div>
75+
<Tablist></Tablist>
76+
<Panellist></Panellist>
77+
</div>
78+
);
79+
};
80+
render(<App></App>, container);
81+
});
82+
return act(() => {
83+
return _api.select('2').then(result => {
84+
expect(typeof result != 'undefined').toBe(true);
85+
return _api.close('2').then(result => {
86+
expect(typeof result != 'undefined').toBe(true);
87+
});
88+
});
89+
});
90+
});
6791
test('opening new tab and closing selected tab', () => {
6892
let _api;
6993
act(() => {
@@ -148,7 +172,7 @@ describe("actions", () => {
148172
});
149173
describe("calling some action inside the events options", () => {
150174
let _api;
151-
test("calling select method inside the onLoad option", () => {
175+
test("select method can be called inside the onLoad option", () => {
152176
const op = {
153177
tabs: [{
154178
id: '1',

src/useDynamicTabs/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import TabList from "../tabList/tabList.js";
3+
import PanelList from "../panelList/panelList.js";
4+
import reducer from "../utils/stateManagement/reducer.js";
5+
import Api from '../utils/api/api.js';
6+
import useDynTabs from './useDynamicTabs.js';
7+
import { ApiContext, StateContext, ForceUpdateContext } from "../utils/context.js";
8+
const getDeps = function () {
9+
const getApiInstance = (options) => {
10+
return new (Api)(options);
11+
};
12+
return { reducer, getApiInstance, PanelList, TabList, ApiContext, StateContext, ForceUpdateContext };
13+
};
14+
export default useDynTabs.bind(null, getDeps);

src/useDynamicTabs/useDynamicTabs.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11

22
import React, { useReducer, useLayoutEffect, useRef } from "react";
3-
import TabList from "../tabList/tabList";
4-
import PanelList from "../panelList/panelList.js";
5-
import reducer from "../utils/stateManagement/reducer";
6-
import Api from '../utils/api';
7-
import { ApiContext, StateContext, ForceUpdateContext } from "../utils/context.js";
8-
function useDynamicTabs(options) {
3+
function useDynamicTabs(getDeps, options = {}) {
4+
const { reducer, getApiInstance, PanelList, TabList, ApiContext, StateContext, ForceUpdateContext } = getDeps();
95
const ref = useRef(null);
106
if (ref.current === null)
11-
ref.current = { api: new (Api)({ options }), TabListComponent: null, PanelListComponent: null };
7+
ref.current = { api: getApiInstance(options), TabListComponent: null, PanelListComponent: null };
128
const { current: { api } } = ref
139
, _ref = ref.current
1410
, [state, dispatch] = useReducer(reducer, api.getInitialState());
15-
api.updateStateRef(state).updateDispatch(dispatch).setPerviousState(state);
11+
api.updateStateRef(state).updateDispatchRef(dispatch);
1612
useLayoutEffect(() => {
1713
api.trigger('onLoad', api.userProxy);
1814
return () => { api.trigger('onDestroy', api.userProxy); };
1915
}, []);
2016
useLayoutEffect(() => {
21-
api.updatePerviousState(state);
17+
api.updateState(state);
2218
}, [state]);
2319
useLayoutEffect(() => {
2420
api.trigger('onInit', api.userProxy);

src/utils/api/baseApi.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import actions from '../stateManagement/actions';
22
import Helper from '../helper.js';
33
function BaseApi(helper) {
44
this._helper = helper;
5-
this._state = null;// it will be updated after execution of useState
5+
this._state = null;// it will be updated after each render
66
this._initialState = null;
77
this._perviousState = null;// it is a pervious value of this._state
88
this._dispatch = null;
99
helper.setNoneEnumProps(this, {
1010
forceUpdateState: {},
11-
stateRef: {}// both of stateRef and state have a same reference. It will be updated in each execution of useDynamicTabs.js
11+
stateRef: {}// have a same reference with state . It will be updated in each execution of useDynamicTabs.js
1212
});
1313
}
1414
BaseApi.prototype._select = function (tabId) { this._dispatch({ type: actions.active, tabId }); };
@@ -19,20 +19,10 @@ BaseApi.prototype._refresh = function () {
1919
this._dispatch({ type: actions.refresh });
2020
};
2121
Helper.setNoneEnumProps(BaseApi.prototype, {
22-
updateStateRef: function (state) {
23-
this.stateRef = state;
24-
return this;
25-
},
26-
updateDispatch: function (dispatch) { this._dispatch = dispatch; return this; },
27-
setPerviousState: function (state) {
28-
if (this._perviousState == null) {
29-
this._perviousState = this._helper.getCopyState(state);
30-
this._state = this._helper.getCopyState(state);
31-
}
32-
return this;
33-
},
34-
updatePerviousState: function (state) {
35-
this._perviousState = this._helper.getCopyState(this._state);
22+
updateStateRef: function (state) { this.stateRef = state; return this; },
23+
updateDispatchRef: function (dispatch) { this._dispatch = dispatch; return this; },
24+
updateState: function (state) {
25+
this._perviousState = this._helper.getCopyState(this._state || state);
3626
this._state = this._helper.getCopyState(state);
3727
return this;
3828
}

0 commit comments

Comments
 (0)