Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Petar Shomov
committed
Nov 27, 2015
1 parent
bb70021
commit ff2325f
Showing
3 changed files
with
174 additions
and
2 deletions.
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
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,81 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using System.Threading.Tasks; | ||
|
||
namespace Reducto.Tests | ||
{ | ||
[TestFixture] | ||
public class ExampleTest | ||
{ | ||
// Actions | ||
|
||
public struct LoginStarted { | ||
public string Username; | ||
} | ||
|
||
public struct LoginFailed {} | ||
|
||
public struct LoginSucceeded { | ||
public string Token; | ||
} | ||
|
||
// State | ||
|
||
public enum LoginStatus { | ||
LoginInProgress, LoggedIn, NotLoggedIn | ||
} | ||
|
||
public struct AppState | ||
{ | ||
public LoginStatus Status; | ||
public String Username; | ||
public String Token; | ||
} | ||
|
||
[Test] | ||
public async void all_in_one_example(){ | ||
var reducer = new SimpleReducer<AppState>() | ||
.When<LoginStarted>((status, action) => { | ||
status.Username = action.Username; | ||
status.Token = ""; | ||
status.Status = LoginStatus.LoginInProgress; | ||
return status; | ||
}) | ||
.When<LoginSucceeded>((status, action) => { | ||
status.Token = action.Token; | ||
status.Status = LoginStatus.LoggedIn; | ||
return status; | ||
}) | ||
.When<LoginFailed>((status, action) => { | ||
status.Status = LoginStatus.NotLoggedIn; | ||
return status; | ||
}); | ||
|
||
var store = new Store<AppState>(reducer); | ||
|
||
var loginAsyncAction = store.asyncAction(async(dispatch, getState) => { | ||
dispatch(new LoginStarted{Username = "John Doe"}); | ||
// faking authentication of user | ||
await Task.Delay(500); | ||
var authenticated = new Random().Next() % 2 == 0; | ||
if (authenticated) { | ||
dispatch(new LoginSucceeded{Token = "1234"}); | ||
} else { | ||
dispatch(new LoginFailed()); | ||
} | ||
return authenticated; | ||
}); | ||
|
||
var logged = await store.Dispatch(loginAsyncAction); | ||
|
||
if (logged){ | ||
Assert.That(store.GetState().Status, Is.EqualTo(LoginStatus.LoggedIn)); | ||
} else { | ||
Assert.That(store.GetState().Status, Is.EqualTo(LoginStatus.NotLoggedIn)); | ||
} | ||
} | ||
} | ||
} | ||
|
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