diff --git a/e2e/playwright/snapshot-tests.spec.ts-snapshots/Zoom-to-fit-on-load---solid-2d-1-Google-Chrome-linux.png b/e2e/playwright/snapshot-tests.spec.ts-snapshots/Zoom-to-fit-on-load---solid-2d-1-Google-Chrome-linux.png index 43daefc12..b895893d4 100644 Binary files a/e2e/playwright/snapshot-tests.spec.ts-snapshots/Zoom-to-fit-on-load---solid-2d-1-Google-Chrome-linux.png and b/e2e/playwright/snapshot-tests.spec.ts-snapshots/Zoom-to-fit-on-load---solid-2d-1-Google-Chrome-linux.png differ diff --git a/src/lang/modifyAst/addFillet.test.ts b/src/lang/modifyAst/addFillet.test.ts index c5b4a5604..0e6b0a992 100644 --- a/src/lang/modifyAst/addFillet.test.ts +++ b/src/lang/modifyAst/addFillet.test.ts @@ -7,10 +7,15 @@ import { Program, CallExpression, } from '../wasm' -import { addFillet, isTagUsedInFillet } from './addFillet' +import { + addFillet, + hasValidFilletSelection, + isTagUsedInFillet, +} from './addFillet' import { getNodeFromPath, getNodePathFromSourceRange } from '../queryAst' import { createLiteral } from 'lang/modifyAst' import { err } from 'lib/trap' +import { Selections } from 'lib/selections' beforeAll(async () => { await initPromise // Initialize the WASM environment before running tests @@ -24,7 +29,7 @@ const runFilletTest = async ( expectedCode: string ) => { const astOrError = parse(code) - if (astOrError instanceof Error) { + if (err(astOrError)) { return new Error('AST not found') } @@ -48,14 +53,14 @@ const runFilletTest = async ( ast, extrudeRange ) - if (pathToExtrudeNode instanceof Error) { + if (err(pathToExtrudeNode)) { return new Error('Path to extrude node not found') } // const radius = createLiteral(5) as Value const result = addFillet(ast, pathToSegmentNode, pathToExtrudeNode, radius) - if (result instanceof Error) { + if (err(result)) { return result } const { modifiedAst } = result @@ -313,3 +318,82 @@ const extrude001 = extrude(-5, sketch001) expect(edges).toEqual([]) }) }) + +describe('Testing button states', () => { + const runButtonStateTest = async ( + code: string, + segmentSnippet: string, + expectedState: boolean + ) => { + // ast + const astOrError = parse(code) + if (err(astOrError)) { + return new Error('AST not found') + } + const ast = astOrError as Program + + // selectionRanges + const range: [number, number] = segmentSnippet + ? [ + code.indexOf(segmentSnippet), + code.indexOf(segmentSnippet) + segmentSnippet.length, + ] + : [ast.end, ast.end] // empty line in the end of the code + + const selectionRanges: Selections = { + codeBasedSelections: [ + { + range, + type: 'default', + }, + ], + otherSelections: [], + } + + // state + const buttonState = hasValidFilletSelection({ + ast, + selectionRanges, + code, + }) + + expect(buttonState).toEqual(expectedState) + } + const codeWithBody: string = ` + const sketch001 = startSketchOn('XY') + |> startProfileAt([-20, -5], %) + |> line([0, 10], %) + |> line([10, 0], %) + |> line([0, -10], %) + |> lineTo([profileStartX(%), profileStartY(%)], %) + |> close(%) + const extrude001 = extrude(-10, sketch001) + ` + const codeWithoutBodies: string = ` + const sketch001 = startSketchOn('XY') + |> startProfileAt([-20, -5], %) + |> line([0, 10], %) + |> line([10, 0], %) + |> line([0, -10], %) + |> lineTo([profileStartX(%), profileStartY(%)], %) + |> close(%) + ` + // body is missing + it('should return false when body is missing and nothing is selected', async () => { + await runButtonStateTest(codeWithoutBodies, '', false) + }) + it('should return false when body is missing and segment is selected', async () => { + await runButtonStateTest(codeWithoutBodies, `line([10, 0], %)`, false) + }) + + // body exists + it('should return true when body exists and nothing is selected', async () => { + await runButtonStateTest(codeWithBody, '', true) + }) + it('should return true when body exists and segment is selected', async () => { + await runButtonStateTest(codeWithBody, `line([10, 0], %)`, true) + }) + it('hould return false when body exists and not a segment is selected', async () => { + await runButtonStateTest(codeWithBody, `close(%)`, false) + }) +})