Skip to content

Commit

Permalink
UIE-153 Environments Storybook and notifications decouple (#4741)
Browse files Browse the repository at this point in the history
  • Loading branch information
msilva-broad authored Apr 2, 2024
1 parent 083ec73 commit 01f55e3
Show file tree
Hide file tree
Showing 41 changed files with 1,099 additions and 258 deletions.
5 changes: 3 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
-

### Testing strategy
<!-- Note that changes impacting components in Storybook stories can be viewed at https://www.chromatic.com/library?appId=65fc89c9335768720ff8605a.
Choose the branch corresponding to this PR, and changes can be reviewed by commit. --->
<!-- Note that changes impacting components in Storybook stories can be viewed at
https://www.chromatic.com/library?appId=65fc89c9335768720ff8605a&branch=<branch>
The branch corresponding to this PR is selected, and changes can be reviewed by commit. --->

- [ ] <!-- Test case 1 -->

Expand Down
85 changes: 85 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'src/style.css';

import type { Preview } from '@storybook/react';
import React from 'react';

Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion lint-staged.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import process from 'process';
import ignore from 'ignore';

export default {
'*.{js,ts}': (files) => {
'*.{js,ts,jsx,tsx}': (files) => {
const cwd = process.cwd();
const relativePaths = files.map((p) => path.relative(cwd, p));

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@terra-ui-packages/analysis": "*",
"@terra-ui-packages/components": "*",
"@terra-ui-packages/core-utils": "*",
"@terra-ui-packages/notifications": "*",
"animate.css": "^4.1.1",
"array-move": "^4.0.0",
"clipboard-polyfill": "^3.0.3",
Expand Down Expand Up @@ -81,6 +82,7 @@
"devDependencies": {
"@axe-core/react": "^4.6.1",
"@pact-foundation/pact": "12.1.1",
"@storybook/addon-actions": "^8.0.0",
"@storybook/addon-essentials": "^7.6.17",
"@storybook/addon-interactions": "^7.6.17",
"@storybook/addon-links": "^7.6.17",
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Color from 'color';
import { mapValues } from 'lodash/fp';
import { createContext, createElement, PropsWithChildren, useContext } from 'react';
import { createContext, createElement, PropsWithChildren, ReactNode, useContext } from 'react';

type ColorPalette = {
/** Used as accent on header, loading spinner, background of beta version tag and some buttons */
Expand Down Expand Up @@ -53,7 +53,7 @@ export type ThemeProviderProps = PropsWithChildren<{
}>;

/** Provides a theme to descendents via React Context. */
export const ThemeProvider = (props: ThemeProviderProps) => {
export const ThemeProvider = (props: ThemeProviderProps): ReactNode => {
const { children, theme } = props;
return createElement(EnrichedThemeContext.Provider, { value: enrichTheme(theme) }, children);
};
Expand Down
6 changes: 6 additions & 0 deletions packages/notifications/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ESLint thinks @terra-ui-packages/test-utils should be listed in dependencies instead of devDependencies.
// TODO: Fix this in ESLint configuration.
// eslint-disable-next-line import/no-extraneous-dependencies
import { getJestConfig } from '@terra-ui-packages/test-utils';

export default getJestConfig();
49 changes: 49 additions & 0 deletions packages/notifications/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@terra-ui-packages/notifications",
"version": "1.0.1",
"scripts": {
"build": "vite build --emptyOutDir",
"dev": "vite build --mode=development --watch",
"test": "jest"
},
"type": "module",
"module": "./lib/es/index.js",
"main": "./lib/cjs/index.cjs",
"types": "./lib/types/index.d.ts",
"exports": {
".": {
"import": "./lib/es/index.js",
"require": "./lib/cjs/index.cjs",
"types": "./lib/types/index.d.ts"
}
},
"files": [
"lib/cjs/**",
"lib/es/**",
"lib/types/**/*.d.ts"
],
"dependencies": {
"@terra-ui-packages/components": "^0.0.6",
"lodash": "^4.17.21"
},
"devDependencies": {
"@terra-ui-packages/build-utils": "^1.0.0",
"@terra-ui-packages/test-utils": "*",
"@testing-library/dom": "^9.3.1",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^28.1.8",
"@types/lodash": "^4.14.184",
"@types/react": "^18.2.15",
"@types/testing-library__jest-dom": "^5.14.9",
"jest": "^27.4.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "~5.1.6",
"vite": "^4.5.2"
},
"peerDependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
}
}
1 change: 1 addition & 0 deletions packages/notifications/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useNotifications';
55 changes: 55 additions & 0 deletions packages/notifications/src/useNotifications.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ErrorBoundary } from '@terra-ui-packages/components';
import { render } from '@testing-library/react';
import React from 'react';

import { NotificationsContract, NotificationsProvider, text, useNotificationsFromContext } from './useNotifications';

describe('useNotificationsFromContext', () => {
it('gets notifications provider from context', () => {
// Arrange
const onRenderWithReporter = jest.fn();

const TestComponent = () => {
const reporter = useNotificationsFromContext();
onRenderWithReporter(reporter);
return null;
};

const reporter: NotificationsContract = {
notify: jest.fn(),
};

// Act
render(
<NotificationsProvider notifications={reporter}>
<TestComponent />
</NotificationsProvider>
);

// Assert
expect(onRenderWithReporter).toHaveBeenCalledWith(expect.objectContaining(reporter));
});

it('throw an error if no notifications provider is provided', () => {
// Arrange
const onRenderError = jest.fn();

const TestComponent = () => {
useNotificationsFromContext();
return null;
};

// Prevent React from logging the error.
jest.spyOn(console, 'error').mockImplementation(() => {});

// Act
render(
<ErrorBoundary onError={onRenderError}>
<TestComponent />
</ErrorBoundary>
);

// Assert
expect(onRenderError).toHaveBeenCalledWith(new Error(text.error.noProvider));
});
});
Loading

0 comments on commit 01f55e3

Please sign in to comment.