Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Aug 19, 2024
1 parent e3ea488 commit 17d90b4
Show file tree
Hide file tree
Showing 30 changed files with 845 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Altinn.Studio.Designer.Exceptions.AppDevelopment;

/// <summary>
/// Indicates that an error occurred during C# code generation.
/// </summary>
[Serializable]
public class ConflictingFileNameException : Exception
{
/// <inheritdoc/>
public ConflictingFileNameException()
{
}

/// <inheritdoc/>
public ConflictingFileNameException(string message) : base(message)
{
}

/// <inheritdoc/>
public ConflictingFileNameException(string message, Exception innerException) : base(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class AppDevelopmentErrorCodes
public const string NonUniqueLayoutSetIdError = "AD_01";
public const string NonUniqueTaskForLayoutSetError = "AD_02";
public const string EmptyLayoutSetIdError = "AD_03";
public const string ConflictingFileNameError = "AD_04";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public override void OnException(ExceptionContext context)
{
context.Result = new ObjectResult(ProblemDetailsUtils.GenerateProblemDetails(context.Exception, AppDevelopmentErrorCodes.EmptyLayoutSetIdError, HttpStatusCode.BadRequest)) { StatusCode = (int)HttpStatusCode.BadRequest };
}
if (context.Exception is ConflictingFileNameException)
{
context.Result = new ObjectResult(ProblemDetailsUtils.GenerateProblemDetails(context.Exception, AppDevelopmentErrorCodes.ConflictingFileNameError, HttpStatusCode.BadRequest)) { StatusCode = (int)HttpStatusCode.BadRequest };
}
}
}
}
4 changes: 2 additions & 2 deletions backend/src/Designer/Services/Implementation/ImagesService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Exceptions.AppDevelopment;
using Altinn.Studio.Designer.Helpers;
using Altinn.Studio.Designer.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task UploadImage(string org, string repo, string developer, string
bool imageExists = altinnAppGitRepository.DoesImageExist(imageName);
if (imageExists && !overrideExisting)
{
throw new InvalidOperationException("An image with this name already exists.");
throw new ConflictingFileNameException("An image with this name already exists.");
}
using MemoryStream imageMemoryStream = new MemoryStream();
imageStream.CopyTo(imageMemoryStream);
Expand Down
11 changes: 7 additions & 4 deletions frontend/packages/shared/src/contexts/ServicesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const handleError = (
// TODO : log axios errors

const renderToast = (key: string, options: ToastOptions = {}) => {
debugger;
const errorMessageKey = `api_errors.${key}`;
if (i18n.exists(errorMessageKey)) {
toast.error(t(errorMessageKey), {
Expand Down Expand Up @@ -66,16 +67,18 @@ const handleError = (
return;
}

if (errorCode) {
return renderToast(errorCode);
}

if (
meta?.hideDefaultError === true ||
(meta?.hideDefaultError instanceof Function && meta?.hideDefaultError?.(error))
)
return;

if (errorCode) {
return renderToast(errorCode);
}

debugger;

renderDefaultToast();
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useMutation } from '@tanstack/react-query';
import { useServicesContext } from 'app-shared/contexts/ServicesContext';

export const useAddImageMutation = (org: string, app: string) => {
export const useAddImageMutation = (
org: string,
app: string,
hideDefaultError: boolean = false,
) => {
const { addImage } = useServicesContext();
return useMutation({
mutationFn: (form: FormData) => addImage(org, app, form),
meta: {
hideDefaultError: true,
hideDefaultError: hideDefaultError,
},
});
};
3 changes: 1 addition & 2 deletions frontend/packages/shared/src/utils/featureToggleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export type SupportedFeatureFlags =
| 'componentConfigBeta'
| 'shouldOverrideAppLibCheck'
| 'resourceMigration'
| 'multipleDataModelsPerTask'
| 'useImageLibrary'; // is `use-` prefix allowed to use for other things than hooks?
| 'multipleDataModelsPerTask';

/*
* Please add all the features that you want to be toggle on by default here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ describe('Properties', () => {
await user.click(button);
expect(button).toHaveAttribute('aria-expanded', 'false');
});
it('Sets accordion title to include images when component is images', async () => {
renderProperties({ formItem: componentMocks[ComponentType.Image] });
const accordionTitle = screen.queryByRole('button', {
name: textMock('right_menu.text_and_image'),
});
expect(accordionTitle).toBeInTheDocument();
});
});

describe('DataModelBindings', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { componentSchemaMocks } from '../../testing/componentSchemaMocks';
import type { ITextResource, ITextResources } from 'app-shared/types/global';
import { DEFAULT_LANGUAGE } from 'app-shared/constants';
import { app, org } from '@studio/testing/testids';
import { componentMocks } from '@altinn/ux-editor/testing/componentMocks';
import { ComponentType } from 'app-shared/types/ComponentType';

// Test data:
const labelTextId = 'labelTextId';
Expand Down Expand Up @@ -57,6 +59,14 @@ describe('TextTab', () => {
render({ props });
});

it('should render sub title for texts', () => {
render({ props });
const textsSubTitle = screen.getByText(
textMock('ux_editor.properties_panel.texts.sub_title_texts'),
);
expect(textsSubTitle).toBeInTheDocument();
});

it('should render all available textResourceBinding properties for the group component', () => {
render({ props });
textResourceBindingsPropertiesForComponentType(props.formItem.type).forEach((trbProperty) => {
Expand Down Expand Up @@ -157,6 +167,40 @@ describe('TextTab', () => {
});
screen.getByRole('button', { name: textMock('ux_editor.modal_new_option') });
});

it('should render image section if component is image', () => {
render({
props: {
...props,
formItem: {
...componentMocks[ComponentType.Image],
},
},
});
const addImageTabTitle = screen.getByRole('tab', {
name: textMock('ux_editor.properties_panel.images.add_image_tab_title'),
});
const pasteUrlTabTitle = screen.getByRole('tab', {
name: textMock('ux_editor.properties_panel.images.enter_external_url_tab_title'),
});
expect(addImageTabTitle).toBeInTheDocument();
expect(pasteUrlTabTitle).toBeInTheDocument();
});

it('should render sub title for images options when component is image', () => {
render({
props: {
...props,
formItem: {
...componentMocks[ComponentType.Image],
},
},
});
const imagesSubTitle = screen.getByText(
textMock('ux_editor.properties_panel.texts.sub_title_images'),
);
expect(imagesSubTitle).toBeInTheDocument();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.alert {
margin: var(--fds-spacing-5);
margin-top: var(--fds-spacing-2);
}
Loading

0 comments on commit 17d90b4

Please sign in to comment.