Skip to content

Commit

Permalink
Update flow.logic with flow.slice api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaCWebDeveloper committed May 22, 2024
1 parent 0e00b27 commit 1a15be7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 120 deletions.
139 changes: 73 additions & 66 deletions packages/flow-client/src/app/redux/modules/flow/flow.logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { MockedFunction } from 'vitest';
import '../../../../../vitest-esbuild-compat';
import { RootState } from '../../store';
import {
NodeEntity,
selectAllNodes,
selectNodeById,
PaletteNodeEntity,
selectAllPaletteNodes,
selectPaletteNodeById,
} from '../palette/node.slice';
import {
FlowLogic,
Expand All @@ -19,21 +19,21 @@ import {
FlowNodeEntity,
SubflowEntity,
flowActions,
selectDirectories,
selectEntityById,
selectAllDirectories,
selectAllFlowEntities,
selectFlowEntityById,
selectFlowNodeById,
selectFlowNodesByFlowId,
selectFlows,
selectSubflows,
} from './flow.slice';

vi.mock('../node/node.slice', async importOriginal => {
vi.mock('../palette/node.slice', async importOriginal => {
const originalModule = await importOriginal<
typeof import('../palette/node.slice')
>();
return {
...originalModule,
selectAllNodes: vi.fn(() => []),
selectNodeById: vi.fn(() => null),
selectAllPaletteNodes: vi.fn(() => []),
selectPaletteNodeById: vi.fn(() => null),
};
});

Expand All @@ -44,35 +44,37 @@ vi.mock('./flow.slice', async importOriginal => {
>();
return {
...originalModule,
selectFlows: vi.fn(() => []),
selectSubflows: vi.fn(() => []),
selectAllFlowEntities: vi.fn(() => []),
selectFlowNodesByFlowId: vi.fn(() => []),
selectEntityById: vi.fn(() => null),
selectDirectories: vi.fn(() => []),
selectFlowEntityById: vi.fn(() => null),
selectFlowNodeById: vi.fn(() => null),
selectAllDirectories: vi.fn(() => []),
};
});

const mockDispatch = vi.fn();
const mockGetState = vi.fn(() => ({})) as unknown as () => RootState;

const mockedSelectAllNodes = selectAllNodes as MockedFunction<
typeof selectAllNodes
const mockedSelectAllPaletteNodes = selectAllPaletteNodes as MockedFunction<
typeof selectAllPaletteNodes
>;
const mockedSelectNodeById = selectNodeById as MockedFunction<
typeof selectNodeById
const mockedSelectPaletteNodeById = selectPaletteNodeById as MockedFunction<
typeof selectPaletteNodeById
>;
const mockedSelectEntityById = selectEntityById as MockedFunction<
typeof selectEntityById
const mockedSelectFlowEntityById = selectFlowEntityById as MockedFunction<
typeof selectFlowEntityById
>;
const mockedSelectFlowNodeById = selectFlowNodeById as MockedFunction<
typeof selectFlowNodeById
>;
const mockedSelectFlowNodesByFlowId = selectFlowNodesByFlowId as MockedFunction<
typeof selectFlowNodesByFlowId
>;
const mockedSelectFlows = selectFlows as MockedFunction<typeof selectFlows>;
const mockedSelectSubflows = selectSubflows as MockedFunction<
typeof selectSubflows
const mockedSelectAllFlowEntities = selectAllFlowEntities as MockedFunction<
typeof selectAllFlowEntities
>;
const mockedSelectDirectories = selectDirectories as MockedFunction<
typeof selectDirectories
const mockedSelectAllDirectories = selectAllDirectories as MockedFunction<
typeof selectAllDirectories
>;

describe('flow.logic', () => {
Expand Down Expand Up @@ -203,10 +205,10 @@ describe('flow.logic', () => {
);

expect(mockDispatch).toHaveBeenCalledWith(
flowActions.upsertEntity(
flowActions.upsertFlowEntity(
expect.objectContaining({
id: 'flow1',
type: 'tab',
type: 'flow',
// Other properties as they should be in the action payload
})
)
Expand All @@ -216,8 +218,8 @@ describe('flow.logic', () => {
it('should not override existing flow properties with new ones', async () => {
const existingFlow = {
id: 'flow1',
label: 'Existing Flow Label',
type: 'tab',
name: 'Existing Flow Label',
type: 'flow',
extras: { detail: 'Existing details' },
disabled: false,
info: '',
Expand All @@ -236,18 +238,18 @@ describe('flow.logic', () => {
selected: false,
} as SerializedGraph;

mockedSelectEntityById.mockReturnValue(existingFlow);
mockedSelectFlowEntityById.mockReturnValue(existingFlow);

await flowLogic.updateFlowFromSerializedGraph(serializedGraph)(
mockDispatch,
mockGetState
);

expect(mockDispatch).toHaveBeenCalledWith(
flowActions.upsertEntity(
flowActions.upsertFlowEntity(
expect.objectContaining({
id: 'flow1',
label: 'Existing Flow Label', // Ensure the label is not overridden
name: 'Existing Flow Label', // Ensure the label is not overridden
extras: { detail: 'Existing details' }, // Ensure extras are not overridden
})
)
Expand Down Expand Up @@ -285,7 +287,7 @@ describe('flow.logic', () => {
);

expect(mockDispatch).toHaveBeenCalledWith(
flowActions.upsertEntities(
flowActions.upsertFlowNodes(
expect.arrayContaining([
expect.objectContaining({
id: 'node1',
Expand Down Expand Up @@ -325,7 +327,7 @@ describe('flow.logic', () => {
locked: false,
selected: false,
extras: {
entity: {} as NodeEntity,
entity: {} as PaletteNodeEntity,
},
ports: [
{
Expand All @@ -352,7 +354,7 @@ describe('flow.logic', () => {
locked: false,
selected: false,
extras: {
entity: {} as NodeEntity,
entity: {} as PaletteNodeEntity,
},
ports: [
{
Expand Down Expand Up @@ -410,7 +412,7 @@ describe('flow.logic', () => {

// Verify that the dispatch was called with actions that reflect the correct wiring
expect(mockDispatch).toHaveBeenCalledWith(
flowActions.upsertEntities(
flowActions.upsertFlowNodes(
expect.arrayContaining([
expect.objectContaining({
id: 'node1',
Expand All @@ -427,7 +429,7 @@ describe('flow.logic', () => {
});

describe('updateFlowNode', () => {
const testNodeEntity: NodeEntity = {
const testNodeEntity: PaletteNodeEntity = {
id: 'node1',
type: 'custom-node',
nodeRedId: 'node1',
Expand Down Expand Up @@ -484,18 +486,18 @@ describe('flow.logic', () => {
outputs: numOutputs,
};
beforeEach(() => {
mockedSelectEntityById.mockImplementation((state, id) => {
mockedSelectFlowNodeById.mockImplementation((state, id) => {
if (id === 'node1') {
return testFlowNodeEntity;
}
return null as unknown as FlowNodeEntity;
});

mockedSelectNodeById.mockImplementation((state, id) => {
mockedSelectPaletteNodeById.mockImplementation((state, id) => {
if (id === 'custom-node') {
return testNodeEntity;
}
return null as unknown as NodeEntity;
return null as unknown as PaletteNodeEntity;
});
});

Expand All @@ -511,7 +513,7 @@ describe('flow.logic', () => {
);

expect(mockDispatch).toHaveBeenCalledWith(
flowActions.updateEntity({
flowActions.updateFlowNode({
id: 'node1',
changes: expect.objectContaining({
inputs: 0,
Expand All @@ -533,7 +535,7 @@ describe('flow.logic', () => {
);

expect(mockDispatch).toHaveBeenCalledWith(
flowActions.updateEntity({
flowActions.updateFlowNode({
id: 'node1',
changes: expect.objectContaining({
outputs: 1,
Expand Down Expand Up @@ -563,7 +565,7 @@ describe('flow.logic', () => {

// Assuming the getNodeInputsOutputs method generates labels "Input 1" and "Output 1"
expect(mockDispatch).toHaveBeenCalledWith(
flowActions.updateEntity({
flowActions.updateFlowNode({
id: 'node1',
changes: expect.objectContaining({
inPorts: expect.arrayContaining([
Expand Down Expand Up @@ -596,7 +598,7 @@ describe('flow.logic', () => {
);

expect(mockDispatch).toHaveBeenCalledWith(
flowActions.updateEntity({
flowActions.updateFlowNode({
id: 'node1',
changes: expect.objectContaining({
inPorts: [], // Expecting no input ports
Expand All @@ -618,7 +620,7 @@ describe('flow.logic', () => {
});

it('correctly serializes a flow with nodes and links', () => {
const mockNodeEntity: NodeEntity = {
const mockNodeEntity: PaletteNodeEntity = {
id: 'node2',
nodeRedId: 'node2',
name: 'Node 2',
Expand All @@ -639,8 +641,8 @@ describe('flow.logic', () => {

const mockFlow = {
id: 'flow1',
type: 'tab',
label: 'My Flow',
type: 'flow',
name: 'My Flow',
disabled: false,
info: '',
env: [],
Expand Down Expand Up @@ -679,8 +681,10 @@ describe('flow.logic', () => {
];

// Mock the selector responses
mockedSelectAllNodes.mockImplementation(() => [mockNodeEntity]);
mockedSelectEntityById.mockImplementation(() => mockFlow);
mockedSelectAllPaletteNodes.mockImplementation(() => [
mockNodeEntity,
]);
mockedSelectFlowEntityById.mockImplementation(() => mockFlow);
mockedSelectFlowNodesByFlowId.mockImplementation(() => mockNodes);

const result = flowLogic.selectSerializedGraphByFlowId.resultFunc(
Expand Down Expand Up @@ -795,18 +799,18 @@ describe('flow.logic', () => {
const customFlows: FlowEntity[] = [
{
id: 'flow3',
label: 'Custom Flow 1',
name: 'Custom Flow 1',
directory: 'custom1',
type: 'tab',
type: 'flow',
disabled: false,
info: '',
env: [],
},
{
id: 'flow4',
label: 'Custom Flow 2',
name: 'Custom Flow 2',
directory: 'custom2',
type: 'tab',
type: 'flow',
disabled: false,
info: '',
env: [],
Expand Down Expand Up @@ -836,9 +840,12 @@ describe('flow.logic', () => {
];

// Mock the selectors
mockedSelectDirectories.mockReturnValue(customDirectories);
mockedSelectFlows.mockReturnValue(customFlows);
mockedSelectSubflows.mockReturnValue(customSubflows);
mockedSelectAllDirectories.mockReturnValue(customDirectories);
mockedSelectAllFlowEntities.mockReturnValue(
(customFlows as (FlowEntity | SubflowEntity)[]).concat(
customSubflows
)
);

const result = flowLogic.selectFlowTree(mockGetState());

Expand Down Expand Up @@ -899,18 +906,18 @@ describe('flow.logic', () => {
const flows: FlowEntity[] = [
{
id: 'flow1',
label: 'Main Flow',
name: 'Main Flow',
directory: 'flows',
type: 'tab',
type: 'flow',
disabled: false,
info: '',
env: [],
},
{
id: 'flow2',
label: 'Secondary Flow',
name: 'Secondary Flow',
directory: 'flows',
type: 'tab',
type: 'flow',
disabled: false,
info: '',
env: [],
Expand Down Expand Up @@ -940,9 +947,10 @@ describe('flow.logic', () => {
];

// Mock the selectors
mockedSelectDirectories.mockReturnValue([]); // No custom directories are provided
mockedSelectFlows.mockReturnValue(flows);
mockedSelectSubflows.mockReturnValue(subflows);
mockedSelectAllDirectories.mockReturnValue([]); // No custom directories are provided
mockedSelectAllFlowEntities.mockReturnValue(
(flows as (FlowEntity | SubflowEntity)[]).concat(subflows)
);

const result = flowLogic.selectFlowTree(mockGetState());

Expand Down Expand Up @@ -998,9 +1006,8 @@ describe('flow.logic', () => {

it('should handle empty directories correctly', () => {
// Mock empty responses
mockedSelectDirectories.mockReturnValue([]);
mockedSelectFlows.mockReturnValue([]);
mockedSelectSubflows.mockReturnValue([]);
mockedSelectAllDirectories.mockReturnValue([]);
mockedSelectAllFlowEntities.mockReturnValue([]);

const result = flowLogic.selectFlowTree(mockGetState());

Expand Down
Loading

0 comments on commit 1a15be7

Please sign in to comment.