Skip to content

Commit

Permalink
Removed State#toStrings method (#4438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Nov 10, 2023
1 parent 3eeb502 commit 7bbf41d
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 172 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-turkeys-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': major
---

Removed `State#toStrings` method.
20 changes: 0 additions & 20 deletions packages/core/src/State.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import isDevelopment from '#is-development';
import { STATE_DELIMITER } from './constants.ts';
import { $$ACTOR_TYPE } from './interpreter.ts';
import { memo } from './memo.ts';
import { MachineSnapshot } from './StateMachine.ts';
Expand Down Expand Up @@ -172,7 +171,6 @@ export class State<
this.context = config.context;
this.historyValue = config.historyValue || {};
this.matches = this.matches.bind(this);
this.toStrings = this.toStrings.bind(this);
this.configuration =
config.configuration ??
Array.from(getConfiguration(getStateNodes(machine.root, config.value)));
Expand All @@ -185,24 +183,6 @@ export class State<
(this as any).error = config.error;
}

/**
* Returns an array of all the string leaf state node paths.
* @param stateValue
* @param delimiter The character(s) that separate each subpath in the string state node path.
*/
public toStrings(stateValue: StateValue = this.value): string[] {
if (typeof stateValue === 'string') {
return [stateValue];
}
const valueKeys = Object.keys(stateValue);

return valueKeys.concat(
...valueKeys.map((key) =>
this.toStrings(stateValue[key]).map((s) => key + STATE_DELIMITER + s)
)
);
}

public toJSON() {
const { configuration, tags, machine, ...jsonValues } = this;

Expand Down
22 changes: 0 additions & 22 deletions packages/core/test/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,28 +219,6 @@ describe('State', () => {
});
});

describe('State.prototype.toStrings', () => {
it('should return all state paths as strings', () => {
const actorRef = createActor(exampleMachine).start();
actorRef.send({
type: 'TO_TWO',
foo: 'test'
});

expect(actorRef.getSnapshot().toStrings()).toEqual([
'two',
'two.deep',
'two.deep.foo'
]);
});

it('should keep reference to state instance after destructuring', () => {
expect(createActor(exampleMachine).getSnapshot().toStrings()).toEqual([
'one'
]);
});
});

describe('status', () => {
it('should show that a machine has not reached its final state', () => {
expect(createActor(exampleMachine).getSnapshot().status).not.toBe('done');
Expand Down
1 change: 0 additions & 1 deletion packages/xstate-solid/src/deriveServiceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const deriveServiceState = <
return {
...state,
toJSON: state.toJSON,
toStrings: state.toStrings,
can: state.can,
hasTag: state.hasTag,
nextEvents: state.nextEvents,
Expand Down
65 changes: 0 additions & 65 deletions packages/xstate-solid/test/useActor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,71 +334,6 @@ describe('useActor', () => {
render(() => <Test />);
});

it('should be reactive to toStrings method calls', () => {
const machine = createMachine({
initial: 'green',
states: {
green: {
on: {
TRANSITION: 'yellow'
}
},
yellow: {
on: {
TRANSITION: 'red'
}
},
red: {
on: {
TRANSITION: 'green'
}
}
}
});

const App = () => {
const service = createActor(machine).start();
const [state, send] = useActor(service);
const [toStrings, setToStrings] = createSignal(state().toStrings());
createEffect(
on(
() => state().value,
() => {
setToStrings(state().toStrings());
}
)
);
return (
<div>
<button
data-testid="transition-button"
onclick={() => send({ type: 'TRANSITION' })}
/>
<div data-testid="to-strings">{JSON.stringify(toStrings())}</div>
</div>
);
};

render(() => <App />);
const toStringsEl = screen.getByTestId('to-strings');
const transitionBtn = screen.getByTestId('transition-button');

// Green
expect(toStringsEl.textContent).toEqual('["green"]');
transitionBtn.click();

// Yellow
expect(toStringsEl.textContent).toEqual('["yellow"]');
transitionBtn.click();

// Red
expect(toStringsEl.textContent).toEqual('["red"]');
transitionBtn.click();

// Green
expect(toStringsEl.textContent).toEqual('["green"]');
});

it('should be reactive to toJSON method calls', () => {
const machine = createMachine({
initial: 'green',
Expand Down
64 changes: 0 additions & 64 deletions packages/xstate-solid/test/useMachine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -713,70 +713,6 @@ describe('useMachine hook', () => {
expect(screen.queryByTestId('event-1')).not.toBeTruthy();
});

it('should be reactive to toStrings method calls', () => {
const machine = createMachine({
initial: 'green',
states: {
green: {
on: {
TRANSITION: 'yellow'
}
},
yellow: {
on: {
TRANSITION: 'red'
}
},
red: {
on: {
TRANSITION: 'green'
}
}
}
});

const App = () => {
const [state, send] = useMachine(machine);
const [toStrings, setToStrings] = createSignal(state.toStrings());
createEffect(
on(
() => state.value,
() => {
setToStrings(state.toStrings());
}
)
);
return (
<div>
<button
data-testid="transition-button"
onclick={() => send({ type: 'TRANSITION' })}
/>
<div data-testid="to-strings">{JSON.stringify(toStrings())}</div>
</div>
);
};

render(() => <App />);
const toStringsEl = screen.getByTestId('to-strings');
const transitionBtn = screen.getByTestId('transition-button');

// Green
expect(toStringsEl.textContent).toEqual('["green"]');
transitionBtn.click();

// Yellow
expect(toStringsEl.textContent).toEqual('["yellow"]');
transitionBtn.click();

// Red
expect(toStringsEl.textContent).toEqual('["red"]');
transitionBtn.click();

// Green
expect(toStringsEl.textContent).toEqual('["green"]');
});

it('should be reactive to toJSON method calls', () => {
const machine = createMachine({
initial: 'green',
Expand Down

0 comments on commit 7bbf41d

Please sign in to comment.