Skip to content

Commit 2df0ef5

Browse files
authored
Merge pull request #20 from madmax983/unsubscribe-failing-test
Unsubscribe failing test
2 parents 389c24c + b5c4ecb commit 2df0ef5

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

force-app/main/default/aura/Redux/Redux.cmp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<aura:component description="Redux">
22
<ltng:require scripts="{!$Resource.redux}"/>
3+
<aura:handler name="destroy" value="{!this}" action="{!c.handleUnsubscribe}"/>
34
<aura:attribute name="name" type="string" default="redux" />
45
<aura:method name="createStore" action="{!c.createStore}">
56
<aura:attribute name="name" type="string" />

force-app/main/default/aura/Redux/ReduxController.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
if(window.reducerQueue && window.reducerQueue[reduxName]) {
1818
combinedReducer = Redux.combineReducers(Object.assign({}, reducerObject, window.reducerQueue[reduxName]));
19+
window.reducerRegistry = {};
1920
window.reducerRegistry[reduxName] = Object.assign({}, window.reducerRegistry[reduxName], reducerObject, window.reducerQueue[reduxName]);
2021
} else if(window.reducerRegistry && window.reducerRegistry[reduxName]) {
2122
combinedReducer = Redux.combineReducers(reducerObject);
@@ -90,7 +91,7 @@
9091
var listener = params ? params.listener : null;
9192

9293
if(listener && window.reduxStore && window.reduxStore[reduxName]) {
93-
window.reduxStore[reduxName].subscribe(listener);
94+
return window.reduxStore[reduxName].subscribe(listener);
9495
}
9596
},
9697

@@ -147,12 +148,7 @@
147148
for(var key in mapStateToAttributes) {
148149
if(mapStateToAttributes.hasOwnProperty(key)) {
149150
if(typeof mapStateToAttributes[key] === "function") {
150-
try {
151-
var returnedFunction = mapStateToAttributes[key]();
152-
target.set(key, returnedFunction(state, target));
153-
} catch(e) {
154-
target.set(key, mapStateToAttributes[key](state, target))
155-
}
151+
target.set(key, mapStateToAttributes[key](state, target))
156152
} else {
157153
target.set(key, state[mapStateToAttributes[key]]);
158154
}
@@ -163,7 +159,7 @@
163159

164160
handleChanges();
165161

166-
window.reduxStore[reduxName].subscribe(handleChanges);
162+
component.unsubscribe = window.reduxStore[reduxName].subscribe(handleChanges);
167163
target.dispatch = window.reduxStore[reduxName].dispatch;
168164
} else {
169165
if(window.subscriberQueue && window.subscriberQueue[reduxName]) {
@@ -179,6 +175,11 @@
179175
}];
180176
}
181177
}
178+
},
179+
handleUnsubscribe: function(component) {
180+
if (component.unsubscribe) {
181+
component.unsubscribe();
182+
}
182183
}
183184

184185
})

force-app/test/default/staticresources/reduxTests.resource

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,58 @@ describe("Redux Tests", function() {
198198
done.fail(e);
199199
});
200200
});
201+
202+
it('unsubscribes when destroyed', function(done) {
203+
var renderInto = document.getElementById("renderTestComponents");
204+
var originalComponent;
205+
var called = 0;
206+
$T.createComponent("c:Redux", {}, renderInto)
207+
.then(function(component) {
208+
originalComponent = component;
209+
function reducer(state, action) {
210+
switch (action.type) {
211+
case 'INCREMENT':
212+
return state + 1;
213+
case 'DECREMENT':
214+
return state - 1;
215+
default:
216+
var defaultState = 0;
217+
return defaultState;
218+
}
219+
}
220+
221+
function handleChanges() {
222+
called++;
223+
}
224+
225+
component.createStore("counter", reducer);
226+
component.unsubscribe = component.subscribe(handleChanges);
227+
component.dispatch({type: "INCREMENT"});
228+
229+
expect(called).toBe(1);
230+
component.dispatch({type: "INCREMENT"});
231+
expect(called).toBe(2);
232+
}).catch(function(e) {
233+
done.fail(e);
234+
});
235+
236+
$T.createComponent("c:Redux", {}, renderInto)
237+
.then(function(component) {
238+
function reducer(state, action) {
239+
return "Hello World"
240+
}
241+
242+
originalComponent.destroy();
243+
component.createStore("helloWorld", reducer);
244+
component.dispatch({type: "INCREMENT"});
245+
246+
component.dispatch({type: "INCREMENT"});
247+
expect(called).toBe(2);
248+
done();
249+
}).catch(function(e) {
250+
done.fail(e);
251+
});
252+
});
201253
});
202254

203255
describe('Test Queueing', function() {

force-app/test/default/staticresources/testutil.resource

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@
114114
while (cmpsToCleanup.length) {
115115
var globalId = cmpsToCleanup.shift();
116116
var cmp = $A.getComponent(globalId);
117-
cmp.destroy();
117+
if(cmp && cmp.isValid()) {
118+
cmp.destroy();
119+
}
118120
}
119121
}
120122

0 commit comments

Comments
 (0)