Skip to content

Instantly share code, notes, and snippets.

@vojtech-cerveny
Created January 28, 2021 22:20
Show Gist options
  • Save vojtech-cerveny/2a42bd0b4c481df949708dfe104c4aee to your computer and use it in GitHub Desktop.
Save vojtech-cerveny/2a42bd0b4c481df949708dfe104c4aee to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const loginMachine = Machine({
initial: "login_opened",
context: {
username: false,
password: false,
registration_done: false,
registration_filled: false,
},
states: {
login_opened: {
on: {
SET_USERNAME: {
target: "login_opened",
actions: assign({ username: true }),
},
SET_PASSWORD: {
target: "login_opened",
actions: assign({ password: true }),
},
SET_REMEMBER_ME: "login_opened",
OPEN_REGISTRATION: {
target: "registration",
actions: assign({ password: false, username: false })
},
SIGN_IN: {
target: "sign_in",
cond: (context) => {
return context.username === true && context.password === true;
},
actions: assign({ password: false, username: false })
},
},
meta: {
test: async () => {
await (await browser.$('[data-test="signin-submit"]')).waitForDisplayed()
expect(await browser.getUrl()).to.equal(`${browser.config.baseUrl}signin`)
// TODO: Add some validations for elements after actions
},
},
},
registration: {
initial: "registration_opened",
states: {
registration_opened: {
on: {
FILL_FORM: {
target: "registration_opened",
actions: assign({ registration_filled: true }),
},
},
},
},
on: {
GO_BACK: { target: "login_opened", actions: assign({ registration_done: false, registration_filled: false }) },
SIGN_UP: {
target: "login_opened",
cond: (context) => context.registration_filled === true,
actions: assign({ registration_done: true, registration_filled: false }),
},
},
meta: {
test: async () => {
await (await browser.$('[data-test="signup-submit"]')).waitForDisplayed()
expect(await browser.getUrl()).to.equal(`${browser.config.baseUrl}signup`)
},
},
},
sign_in: {
on: {
LOGOUT: "login_opened",
},
meta: {
test: async () => {
await browser.waitUntil(async () => await browser.getUrl() == browser.config.baseUrl)
expect(await (await browser.$('[data-test="main"]')).isDisplayed()).to.be.true
// TODO: Add some validations for elements after actions
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment