From ebd9c5111eae7115d9ddbd627eb3c99fa97a018f Mon Sep 17 00:00:00 2001
From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com>
Date: Fri, 2 Aug 2024 20:52:44 +0200
Subject: [PATCH] tests: flexible strategy component stickiness and groupId
(#7735)
Follow-up for #7713
---
.../FlexibleStrategy.test.tsx | 76 ++++++++++++++++++-
.../FlexibleStrategy/FlexibleStrategy.tsx | 37 +++++----
2 files changed, 93 insertions(+), 20 deletions(-)
diff --git a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.test.tsx b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.test.tsx
index 89a4694fcc..325fb88514 100644
--- a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.test.tsx
+++ b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.test.tsx
@@ -1,5 +1,6 @@
+import { vi } from 'vitest';
import { useState } from 'react';
-import { screen, fireEvent } from '@testing-library/react';
+import { screen, fireEvent, waitFor } from '@testing-library/react';
import FlexibleStrategy from './FlexibleStrategy';
import { render } from 'utils/testRenderer';
import { Route, Routes } from 'react-router-dom';
@@ -9,6 +10,7 @@ const server = testServerSetup();
const setupApi = () => {
testServerRoute(server, '/api/admin/projects/default', {});
+ testServerRoute(server, '/api/admin/projects/default/overview', {});
};
test('manipulates the rollout slider', async () => {
@@ -61,3 +63,75 @@ test('manipulates the rollout slider', async () => {
expect(slider).toHaveValue('50');
expect(groupIdInput).toHaveValue('newGroupId');
});
+
+test('if stickiness or groupId not present, fill it with defaults', async () => {
+ const updateParameter = vi.fn();
+ const Wrapper = () => (
+
+
+ }
+ />
+
+ );
+
+ setupApi();
+
+ render(, {
+ route: '/projects/default/features/test',
+ });
+
+ await waitFor(() => {
+ expect(updateParameter).toHaveBeenCalledWith('stickiness', 'default');
+ expect(updateParameter).toHaveBeenCalledWith('groupId', 'test');
+ });
+});
+
+test('displays groupId error', async () => {
+ const Wrapper = () => (
+
+ {}}
+ context={{}}
+ editable={true}
+ errors={
+ {
+ getFormError: () => 'Field required test',
+ } as any
+ }
+ />
+ }
+ />
+
+ );
+
+ setupApi();
+
+ render(, {
+ route: '/projects/default/features/test',
+ });
+
+ await waitFor(async () => {
+ const errorText = await screen.queryByText('Field required test');
+ expect(errorText).toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.tsx b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.tsx
index f3b4236740..e4988b575e 100644
--- a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.tsx
+++ b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/FlexibleStrategy.tsx
@@ -1,3 +1,4 @@
+import { useMemo } from 'react';
import { Box, styled } from '@mui/material';
import type { IFeatureStrategyParameters } from 'interfaces/strategy';
import RolloutSlider from '../RolloutSlider/RolloutSlider';
@@ -13,7 +14,6 @@ import {
import { StickinessSelect } from './StickinessSelect/StickinessSelect';
import { useDefaultProjectSettings } from 'hooks/useDefaultProjectSettings';
import Loader from '../../../common/Loader/Loader';
-import { useEffect, useMemo } from 'react';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import { useLocation } from 'react-router';
import type { IFormErrors } from 'hooks/useFormErrors';
@@ -63,9 +63,6 @@ const FlexibleStrategy = ({
const { pathname } = useLocation();
const isDefaultStrategyEdit = pathname.includes('default-strategy');
- const onUpdate = (field: string) => (newValue: string) => {
- updateParameter(field, newValue);
- };
const updateRollout = (e: Event, value: number | number[]) => {
updateParameter('rollout', value.toString());
@@ -77,22 +74,20 @@ const FlexibleStrategy = ({
: 100;
const stickiness = useMemo(() => {
- if (parameters.stickiness === '' && !loading) {
- return defaultStickiness;
+ if (!parameters.stickiness && !loading) {
+ updateParameter('stickiness', defaultStickiness);
}
- return parseParameterString(parameters.stickiness || defaultStickiness);
- }, [loading, parameters.stickiness]);
+ return parseParameterString(parameters.stickiness);
+ }, [loading, defaultStickiness, parameters.stickiness]);
- if (parameters.stickiness === '') {
- onUpdate('stickiness')(stickiness);
- }
-
- useEffect(() => {
- if (!parameters.groupId) {
- onUpdate('groupId')(isDefaultStrategyEdit ? '' : featureId);
+ const groupId = useMemo(() => {
+ if (!parameters.groupId && !loading) {
+ updateParameter('groupId', isDefaultStrategyEdit ? '' : featureId);
}
- }, [isDefaultStrategyEdit, featureId]);
+
+ return parseParameterString(parameters.groupId);
+ }, [parameters.groupId, isDefaultStrategyEdit, featureId, loading]);
if (loading) {
return ;
@@ -113,7 +108,9 @@ const FlexibleStrategy = ({
value={stickiness}
editable={editable}
dataTestId={FLEXIBLE_STRATEGY_STICKINESS_ID}
- onChange={(e) => onUpdate('stickiness')(e.target.value)}
+ onChange={(e) =>
+ updateParameter('stickiness', e.target.value)
+ }
/>
@@ -121,9 +118,11 @@ const FlexibleStrategy = ({
label='groupId'
sx={{ width: '100%' }}
id='groupId-input'
- value={parseParameterString(parameters.groupId)}
+ value={groupId}
disabled={!editable}
- onChange={(e) => onUpdate('groupId')(e.target.value)}
+ onChange={(e) =>
+ updateParameter('groupId', e.target.value)
+ }
data-testid={FLEXIBLE_STRATEGY_GROUP_ID}
error={Boolean(errors?.getFormError('groupId'))}
helperText={errors?.getFormError('groupId')}