Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@iFwu
Created August 16, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iFwu/1abe1fa124ed0fdea4fd96e5bf55ad89 to your computer and use it in GitHub Desktop.
Save iFwu/1abe1fa124ed0fdea4fd96e5bf55ad89 to your computer and use it in GitHub Desktop.
import { Machine, assign } from 'xstate';
export const todoViewMachine = Machine({
id: 'Todo',
context: {
todosFilter: 'All',
title: '',
index: 0,
},
on: {
FILTER_CHANGE: {
target: 'Unknown.hist',
actions: assign({
todosFilter: (_, event) => event.value,
}),
},
'MARK.Completed': 'Unknown.Completed',
'MARK.Active': 'Unknown.Active',
},
initial: 'Unknown',
states: {
Unknown: {
initial: 'Active',
states: {
hist: {
type: 'history',
target: 'Active',
},
Completed: {
on: {
'': [
{
target: '#Todo.ShownCompleted',
cond: ({ todosFilter }) => todosFilter !== 'Active',
},
{ target: '#Todo.HiddenCompleted' },
],
},
},
Active: {
on: {
'': [
{
target: '#Todo.ShownActive',
cond: ({ todosFilter }) => todosFilter !== 'Completed',
},
{ target: '#Todo.HiddenActive' },
],
},
},
},
},
HiddenCompleted: {
on: { CLEAR_COMPLETED: 'Deleted' },
meta: {
test: () => {
cy.get('.todo-list li').should('not.exist');
cy.get('.main').should('be.visible');
cy.get('.footer').should('be.visible');
},
},
},
HiddenActive: {
meta: {
test: () => {
cy.get('.todo-list li').should('not.exist');
cy.get('.main').should('be.visible');
cy.get('.footer').should('be.visible');
},
},
},
ShownCompleted: {
on: {
TODO_TOGGLE: 'Unknown.Active',
TODO_EDIT: 'Editing',
CLEAR_COMPLETED: 'Deleted',
TODO_DELETE: 'Deleted',
},
meta: {
test: () => {
cy.get('.todo-list li').eq(0).should('have.class', 'completed');
cy.get('.main').should('be.visible');
cy.get('.footer').should('be.visible');
},
},
},
ShownActive: {
on: {
TODO_TOGGLE: 'Unknown.Completed',
TODO_EDIT: 'Editing',
TODO_DELETE: 'Deleted',
},
meta: {
test: () => {
cy.get('.todo-list li').eq(0).should('not.have.class', 'completed');
cy.get('.main').should('be.visible');
cy.get('.footer').should('be.visible');
},
},
},
Editing: {
on: {
CANCEL_EDIT: 'Unknown.hist',
TODO_UPDATE: 'Unknown.hist',
},
meta: {
test: () => {
cy.get('.todo-list li').should('have.class', 'editing');
cy.get('.main').should('be.visible');
cy.get('.footer').should('be.visible');
},
},
},
Deleted: {
type: 'final',
meta: {
test: () => {
cy.get('.todo-list li').should('not.exist');
cy.get('.main').should('not.exist');
cy.get('.footer').should('not.exist');
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment