From 2351726034e40b21673f98634e7c6bf36fbe23ed Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Tue, 26 May 2020 15:44:19 +0200 Subject: [PATCH] :white_check_mark: corrige les tests --- .../cypress/integration/publi.codes/index.js | 3 +- .../source/sites/publi.codes/Studio.tsx | 52 +++++++++++-------- mon-entreprise/source/types/app-env.d.ts | 2 +- publicodes/source/components/rule/Rule.tsx | 5 +- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/mon-entreprise/cypress/integration/publi.codes/index.js b/mon-entreprise/cypress/integration/publi.codes/index.js index dc1561dc2..7c376ca87 100644 --- a/mon-entreprise/cypress/integration/publi.codes/index.js +++ b/mon-entreprise/cypress/integration/publi.codes/index.js @@ -8,8 +8,7 @@ describe('Navigation', function() { }) it('bac à sable should work', function() { cy.contains('Bac à sable').click() - cy.contains('dépenses primeur') - cy.contains('Résultats') + cy.contains('Dépenses primeur') cy.contains('11,50') }) }) diff --git a/mon-entreprise/source/sites/publi.codes/Studio.tsx b/mon-entreprise/source/sites/publi.codes/Studio.tsx index 6d3e4ce4d..dae8b26f9 100644 --- a/mon-entreprise/source/sites/publi.codes/Studio.tsx +++ b/mon-entreprise/source/sites/publi.codes/Studio.tsx @@ -4,7 +4,7 @@ import { invertObj, last } from 'ramda' import React, { useCallback, useEffect, useMemo, useState } from 'react' import emoji from 'react-easy-emoji' import MonacoEditor from 'react-monaco-editor' -import { useHistory, useLocation } from 'react-router-dom' +import { useHistory, useLocation, Redirect } from 'react-router-dom' import styled from 'styled-components' import yaml from 'yaml' @@ -55,19 +55,11 @@ export default function Studio() { const [editorValue, setEditorValue] = useState(initialValue) const debouncedEditorValue = useDebounce(editorValue, 1000) - const targets = useMemo(() => { - try { - return Object.keys(yaml.parse(debouncedEditorValue) ?? {}) - } catch (e) { - console.error(e) - return [] - } - }, [debouncedEditorValue]) - const history = useHistory() useEffect(() => { history.replace({ pathname, + state: { useDefaultValues: true }, search: `?code=${encodeURIComponent(debouncedEditorValue)}` }) }, [debouncedEditorValue, history]) @@ -96,11 +88,7 @@ export default function Studio() { {/* TODO: prévoir de changer la signature de EngineProvider */} - + @@ -108,27 +96,42 @@ export default function Studio() { } type ResultsProps = { - targets: string[] rules: string onClickShare: React.MouseEventHandler } -export const Results = ({ targets, onClickShare, rules }: ResultsProps) => { +export const Results = ({ onClickShare, rules }: ResultsProps) => { + const targets = useMemo(() => { + try { + return Object.keys(yaml.parse(rules) ?? {}) + } catch (e) { + console.error(e) + return [] + } + }, [rules]) const engine = useMemo(() => new Engine(rules), [rules]) const documentationPath = '/studio' - const pathToRule = useMemo( + const pathToRules = useMemo( () => getDocumentationSiteMap({ engine, documentationPath }), [engine, documentationPath] ) - const ruleToPaths = useMemo(() => invertObj(pathToRule), [pathToRule]) + const ruleToPaths = useMemo(() => invertObj(pathToRules), [pathToRules]) const { search, pathname } = useLocation() - const currentTarget = pathToRule[pathname] ?? (last(targets) as string) const history = useHistory() const setCurrentTarget = useCallback( - target => history.replace({ pathname: ruleToPaths[target], search }), + target => + history.replace({ + pathname: ruleToPaths[target], + search, + state: { useDefaultValues: true } + }), [ruleToPaths, history, search] ) - + useEffect(() => { + if (!pathToRules[pathname]) { + setCurrentTarget(last(targets)) + } + }) // EN ATTENDANT d'AVOIR une meilleure gestion d'erreur, on va mocker // console.warn const warnings: string[] = [] @@ -160,7 +163,7 @@ export const Results = ({ targets, onClickShare, rules }: ResultsProps) => { @@ -249,6 +252,9 @@ class ErrorBoundary extends React.Component { {this.state.error.name}
{nl2br(this.state.error.message)} +
+
+ window.location.reload()}>Rafraichir ) } diff --git a/mon-entreprise/source/types/app-env.d.ts b/mon-entreprise/source/types/app-env.d.ts index 4555350e5..9884aa9fd 100644 --- a/mon-entreprise/source/types/app-env.d.ts +++ b/mon-entreprise/source/types/app-env.d.ts @@ -2,7 +2,7 @@ declare namespace NodeJS { interface ProcessEnv { EN_SITE: string FR_SITE: string - NODE_ENV: 'development' | 'production' + NODE_ENV: 'development' | 'production' | 'test' ANALYZE_BUNDLE: '0' | '1' // Netlify variables diff --git a/publicodes/source/components/rule/Rule.tsx b/publicodes/source/components/rule/Rule.tsx index 5e1b0a397..4b1eb7090 100644 --- a/publicodes/source/components/rule/Rule.tsx +++ b/publicodes/source/components/rule/Rule.tsx @@ -17,10 +17,13 @@ export default function Rule({ engine, language }) { + const [viewSource, setViewSource] = useState(false) + if (!engine.getParsedRules()[dottedName]) { + return

Cette règle est introuvable dans la base

+ } const rule = engine.evaluate(dottedName, { useDefaultValues }) - const [viewSource, setViewSource] = useState(false) const { description, question } = rule return (