Skip to content

Instantly share code, notes, and snippets.

@Jyrno42
Last active August 29, 2015 14:23
Show Gist options
  • Save Jyrno42/1698a78969517a3d1c01 to your computer and use it in GitHub Desktop.
Save Jyrno42/1698a78969517a3d1c01 to your computer and use it in GitHub Desktop.
Problem
import React from 'react';
import Alt from 'alt';
import AltContainer from 'alt/AltContainer.js';
import AltIso from 'alt/utils/AltIso';
let alt = new Alt();
const CurrentUserActions = alt.generateActions('receivedCurrentUser', 'failedCurrentUser');
const CurrentUserSource = {
getCurrentUser() {
return {
remote(state, props) {
return new Promise((resolve, reject) => {
//console.log('promise called', props.id);
if (props.id === 1) {
reject('fail');
}
else if (props.id === 2) {
throw new Error('fail');
}
else {
setTimeout(() => resolve({email: 'r@r.ee', 'name': 'John Smith', is_staff: true}), 5);
}
});
},
local(state) {
//return state.isAuthenticated ? state : null;
return null;
},
// here we setup some actions to handle our response
success: CurrentUserActions.receivedCurrentUser,
error: CurrentUserActions.failedCurrentUser
};
}
};
class CurrentUserStore {
constructor() {
this.registerAsync(CurrentUserSource);
this.bindActions(CurrentUserActions);
}
receivedCurrentUser(user) {
//console.log('receivedCurrentUser', user);
this.isAuthenticated = true;
this.profile = user;
}
failedCurrentUser() {
//console.log('failedCurrentUser');
this.isAuthenticated = false;
this.profile = {};
}
}
var userStore = alt.createStore(CurrentUserStore, 'CurrentUserStore');
const User = AltIso.define((props) => {
console.log('props', props);
return userStore.getCurrentUser(props);
}, class b extends React.Component {
render() {
return (
<AltContainer store={userStore} render={props => {
return (
<div>
<span>{'login:' + (props.isAuthenticated === true ? 'true' : props.isAuthenticated === false ? 'false' : 'null')}</span>
<span>{'name:' + (props.profile && props.profile.name || '')}</span>
</div>
);
}} />
);
}
});
import assert from 'assert';
describe('testUserFail', function() {
it('1 not logged in', function(done) {
alt.flush();
AltIso.render(alt, User, {id: 1}).then((markup) => {
//console.log(markup);
assert.notEqual(markup.indexOf('login:false'), -1);
done();
}).catch((err) => {
done(err);
});
});
it('2 not logged in', function(done) {
alt.flush();
AltIso.render(alt, User, {id: 2}).then((markup) => {
//console.log(markup);
assert.notEqual(markup.indexOf('login:false'), -1);
done();
}).catch((err) => {
done(err);
});
});
it('3 is logged in', function(done) {
alt.flush();
AltIso.render(alt, User, {id: 3}).then((markup) => {
//console.log(markup);
assert.notEqual(markup.indexOf('login:true'), -1);
assert.notEqual(markup.indexOf('name:John Smith'), -1);
done();
}).catch((err) => {
done(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment