Skip to content

Commit

Permalink
Add news to repo and change folder structure (#13238)
Browse files Browse the repository at this point in the history
* Import news locally from repo instead of from cdn
  • Loading branch information
standeren authored Aug 14, 2024
1 parent 92fdf44 commit ff116f2
Show file tree
Hide file tree
Showing 36 changed files with 85 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { screen, waitForElementToBeRemoved } from '@testing-library/react';
import { DeploymentContainer } from './DeploymentContainer';
import { APP_DEVELOPMENT_BASENAME } from 'app-shared/constants';
import { renderWithProviders } from '../../../test/testUtils';
import { renderWithProviders } from '../../../../../test/testUtils';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { environment, repository } from 'app-shared/mocks/mocks';
import { app, org } from '@studio/testing/testids';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { screen } from '@testing-library/react';
import type { DeploymentLogListProps } from './DeploymentLogList';
import { DeploymentLogList } from './DeploymentLogList';
import { APP_DEVELOPMENT_BASENAME } from 'app-shared/constants';
import { renderWithProviders } from '../../../test/testUtils';
import { renderWithProviders } from '../../../../../test/testUtils';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { pipelineDeployment, environment } from 'app-shared/mocks/mocks';
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { screen } from '@testing-library/react';
import type { DeploymentStatusProps } from './DeploymentStatus';
import { DeploymentStatus } from './DeploymentStatus';
import { APP_DEVELOPMENT_BASENAME } from 'app-shared/constants';
import { renderWithProviders } from '../../../test/testUtils';
import { renderWithProviders } from '../../../../../test/testUtils';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { kubernetesDeployment } from 'app-shared/mocks/mocks';
import { app, org } from '@studio/testing/testids';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DeploymentContainer';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { screen, waitForElementToBeRemoved } from '@testing-library/react';
import { Deployments } from './Deployments';
import { APP_DEVELOPMENT_BASENAME } from 'app-shared/constants';
import { renderWithProviders } from '../../../test/testUtils';
import { renderWithProviders } from '../../../../test/testUtils';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { repository } from 'app-shared/mocks/mocks';
import { app, org } from '@studio/testing/testids';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { RepoOwnedByPersonInfo } from './RepoOwnedByPersonInfo';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Deployments } from './Deployments';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { screen } from '@testing-library/react';
import { Documentation } from './Documentation';
import { APP_DEVELOPMENT_BASENAME } from 'app-shared/constants';
import { renderWithProviders } from '../../../test/testUtils';
import { renderWithProviders } from '../../../../test/testUtils';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { app, org } from '@studio/testing/testids';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Documentation } from './Documentation';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Navigation } from './Navigation';
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import { screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
import { renderWithProviders } from 'app-development/test/testUtils';
import { APP_DEVELOPMENT_BASENAME, NEWS_EXPIRATION_TIME_IN_DAYS } from 'app-shared/constants';
import { News } from './News';
import { render, screen, waitFor } from '@testing-library/react';
import type { NewsList } from 'app-shared/types/api/NewsList';
import { textMock } from '@studio/testing/mocks/i18nMock';
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext';
import { NEWS_EXPIRATION_TIME_IN_DAYS } from 'app-shared/constants';

const mockNewsData = (newsList: NewsList) => {
jest.mock('./NewsContent/news.nb.json', () => ({
__esModule: true,
default: newsList,
}));
};

const formatDate = (date) => {
const year = date.getFullYear();
Expand All @@ -21,6 +25,9 @@ const formatDateToVisualText = (date: string) => {
};

describe('News', () => {
afterEach(() => {
jest.resetModules();
});
it('section title is always rendered', async () => {
const publishDate = new Date();
const newsList: NewsList = {
Expand All @@ -32,7 +39,7 @@ describe('News', () => {
},
],
};
await render(newsList);
await renderNews(newsList);

await screen.findByText('title');
});
Expand All @@ -47,7 +54,7 @@ describe('News', () => {
},
],
};
await render(newsList);
await renderNews(newsList);

await screen.findByText('title');
await screen.findByText('News content');
Expand All @@ -68,32 +75,14 @@ describe('News', () => {
},
],
};
await render(newsList);
await waitForElementToBeRemoved(() =>
screen.queryByText(textMock('overview.fetch_news_loading_message')),
);
await renderNews(newsList);

const noNewsTitle = screen.getByText(textMock('overview.no_news_title'));
expect(noNewsTitle).toBeInTheDocument();
const noNewsContent = screen.getByText(textMock('overview.no_news_content'));
expect(noNewsContent).toBeInTheDocument();
});

it('loading spinner is shown while waiting for content', async () => {
const newsList: NewsList = {
news: [
{
title: 'title',
content: 'News content',
date: '2021-01-01',
},
],
};
render(newsList);

await screen.findByText(textMock('overview.fetch_news_loading_message'));
});

it('does not list a news if the date in the news is in the future', async () => {
const publishDate = new Date();
publishDate.setDate(publishDate.getDate() + 1);
Expand All @@ -106,8 +95,7 @@ describe('News', () => {
},
],
};

await render(newsList);
await renderNews(newsList);

await waitFor(() => {
screen.queryByText('News content');
Expand All @@ -129,8 +117,7 @@ describe('News', () => {
},
],
};

await render(newsList);
await renderNews(newsList);

await waitFor(() => {
screen.queryByText('News content');
Expand All @@ -139,32 +126,10 @@ describe('News', () => {
const news = screen.queryByText('News content');
expect(news).not.toBeInTheDocument();
});

it('error message is shown when content fails to load', async () => {
const publishDate = new Date();
const newsList: NewsList = {
news: [
{
title: 'title',
content: 'News content',
date: formatDate(publishDate),
},
],
};
await render(newsList, {
getNewsList: jest.fn().mockImplementation(() => Promise.reject()),
});

await screen.findByText(textMock('overview.fetch_news_error_message'));
});
});

const render = async (newsList: NewsList, queries?: Partial<ServicesContextProps>) => {
return renderWithProviders(<News />, {
startUrl: `${APP_DEVELOPMENT_BASENAME}/my-org/my-app`,
queries: {
getNewsList: jest.fn().mockImplementation(() => Promise.resolve<NewsList>(newsList)),
...queries,
},
});
const renderNews = async (newsList: NewsList) => {
mockNewsData(newsList);
const { News } = await import('./News');
return render(<News />);
};
Original file line number Diff line number Diff line change
@@ -1,57 +1,37 @@
import * as React from 'react';
import { Card, Heading, Link, Paragraph } from '@digdir/designsystemet-react';
import { useNewsListQuery } from 'app-development/hooks/queries/useNewsListQuery';
import { StudioPageError, StudioSpinner } from '@studio/components';
import { useTranslation } from 'react-i18next';
import classes from './News.module.css';
import { NEWS_EXPIRATION_TIME_IN_DAYS } from 'app-shared/constants';
import { gitHubRoadMapUrl } from 'app-shared/ext-urls';
import newsData from './NewsContent/news.nb.json';
import { NEWS_EXPIRATION_TIME_IN_DAYS } from 'app-shared/constants';

export const News = () => {
const { data: newsList, isPending, isError, isSuccess } = useNewsListQuery();
const { t } = useTranslation();
const today = new Date();

const newsList = newsData.news;

const showNews = (date: string): boolean => {
const publishDate = new Date(date);
const publishDatePlus30Days = new Date(publishDate);
publishDatePlus30Days.setDate(publishDate.getDate() + NEWS_EXPIRATION_TIME_IN_DAYS);
return publishDate <= today && today <= publishDatePlus30Days;
};

const thereAreRelevantNews =
isSuccess && newsList.news.filter((news) => showNews(news.date)).length > 0;
const thereAreRelevantNews = newsList.filter((news) => showNews(news.date)).length > 0;

const formatDateToText = (date: string) => {
// Date comes in this format: YYYY-MM-DD
const [year, month, day] = date.split('-');
return `${day}.${month}.${year}`;
};

if (isPending) {
return (
<NewsTemplate>
<StudioSpinner showSpinnerTitle spinnerTitle={t('overview.fetch_news_loading_message')} />
</NewsTemplate>
);
}

if (isError) {
return (
<NewsTemplate>
<StudioPageError
title={t('overview.news_error_title')}
message={t('overview.fetch_news_error_message')}
/>
</NewsTemplate>
);
}

return (
<div>
<NewsTemplate>
{thereAreRelevantNews ? (
newsList.news?.map(({ title, content, date }) => {
newsList?.map(({ title, content, date }) => {
return (
showNews(date) && (
<Card className={classes.newsContent} key={title}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "news.schema.json",
"news": [
{
"date": "2024-08-07",
"title": "Prosessverktøyet støtter nå betaling",
"content": "Du kan nå legge til betalingssteg i prosessen."
},
{
"date": "2024-07-18",
"title": "Vi har fjernet regler for beregninger og betingede visninger",
"content": "Verktøyene er utdaterte, så vi har tatt de bort fra Lage-siden. Du kan fortsatt lage regler for beregninger og betingede visninger i koden, men vi anbefaler å heller bruke dynamiske uttrykk."
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$id": "news.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Altinn Studio news schema",
"description": "Schema that describes the structure in which news items are stored in Altinn Studio",
"type": "object",
"properties": {
"news": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"date": {
"type": "string",
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
"description": "The date the news item was published"
},
"title": {
"type": "string",
"description": "The title of the news item"
},
"content": {
"type": "string",
"description": "The content of the news item"
}
},
"required": ["date", "title", "content"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { News } from './News';
3 changes: 0 additions & 3 deletions frontend/language/src/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,6 @@
"overview.download_repo_changes": "Jeg vil ha en zip-fil med mine endrede filer",
"overview.download_repo_full": "Jeg vil ha en zip-fil med hele repoet",
"overview.download_repo_heading": "Last ned alle endringer?",
"overview.fetch_news_error_message": "Kunne ikke laste inn nyheter. Prøv igjen senere.",
"overview.fetch_news_loading_message": "Laster nyheter...",
"overview.fetch_title_error_message": "Kunne ikke laste inn tittelen for denne appen. Prøv igjen senere.",
"overview.go_to_publish": "Gå til <0>Publiser</0>.",
"overview.header_loading": "Laster inn navnet på appen",
"overview.navigation_title": "Bygg appen med våre verktøy",
Expand Down

0 comments on commit ff116f2

Please sign in to comment.