Skip to content

Commit 44f8882

Browse files
committed
Add useReduceActivities
1 parent e0e965b commit 44f8882

14 files changed

+491
-141
lines changed

.eslintrc.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ parserOptions:
1919
sourceType: module
2020

2121
overrides:
22-
- files:
23-
- '__tests__/**/*.js'
24-
- '*.spec.js'
25-
- '*.test.js'
26-
27-
env:
28-
jest: true
29-
3022
- files:
3123
- 'jest.config.js'
3224
- 'jest.*.config.js'
@@ -85,6 +77,24 @@ overrides:
8577
# Shorthanding if-condition with && and ||.
8678
'@typescript-eslint/no-unused-expressions': off
8779

80+
- files:
81+
- '__tests__/**/*.js'
82+
- '**/*.spec.js'
83+
- '**/*.spec.jsx'
84+
- '**/*.spec.ts'
85+
- '**/*.spec.tsx'
86+
- '**/*.test.js'
87+
- '**/*.test.jsx'
88+
- '**/*.test.ts'
89+
- '**/*.test.tsx'
90+
91+
env:
92+
jest: true
93+
94+
rules:
95+
'@typescript-eslint/no-require-imports': off
96+
no-magic-numbers: off
97+
8898
rules:
8999
# Only list rules that are not in *:recommended set
90100
# If rules are set to disable the one in *:recommended, please elaborate the reason

__tests__/hooks/useEmitTypingIndicator.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

__tests__/hooks/useSendTypingIndicator.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

__tests__/html/typingIndicator.liveRegion.multiple.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en-US">
33
<head>
44
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
@@ -8,7 +8,16 @@
88
</head>
99
<body>
1010
<main id="webchat"></main>
11-
<script>
11+
<script type="importmap">
12+
{
13+
"imports": {
14+
"@testduet/wait-for": "https://esm.sh/@testduet/wait-for"
15+
}
16+
}
17+
</script>
18+
<script type="module">
19+
import { waitFor } from '@testduet/wait-for';
20+
1221
run(async function () {
1322
const clock = lolex.createClock();
1423
const directLine = testHelpers.createDirectLineWithTranscript([], { ponyfill: clock });
@@ -36,11 +45,7 @@
3645
};
3746

3847
const liveRegionTypingIndicatorBecame = expected =>
39-
pageConditions.became(
40-
'typing indicator to appear in live region',
41-
() => liveRegionInnerTexts.join('\n') === expected.join('\n'),
42-
2000
43-
);
48+
waitFor(() => expect(liveRegionInnerTexts.join('\n')).toEqual(expected.join('\n')), 2_000);
4449

4550
WebChat.renderWebChat(
4651
{

__tests__/html/typingIndicator.liveRegion.simple.html

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en-US">
33
<head>
44
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
@@ -8,7 +8,16 @@
88
</head>
99
<body>
1010
<main id="webchat"></main>
11-
<script>
11+
<script type="importmap">
12+
{
13+
"imports": {
14+
"@testduet/wait-for": "https://esm.sh/@testduet/wait-for"
15+
}
16+
}
17+
</script>
18+
<script type="module">
19+
import { waitFor } from '@testduet/wait-for';
20+
1221
run(async function () {
1322
const clock = lolex.createClock();
1423
const directLine = testHelpers.createDirectLineWithTranscript([], { ponyfill: clock });
@@ -62,11 +71,7 @@
6271
});
6372

6473
// THEN: It should read "Bot is typing."
65-
await pageConditions.became(
66-
'typing indicator to appear in live region',
67-
async () => () => liveRegionInnerTexts.join('\n') === 'Bot is typing.',
68-
5000
69-
);
74+
await waitFor(() => expect(liveRegionInnerTexts.join('\n')).toBe('Bot is typing.'));
7075

7176
// THEN: The typing indicator should be shown.
7277
await pageConditions.typingIndicatorShown();
@@ -86,11 +91,7 @@
8691
});
8792

8893
// THEN: It should not add any live region elements.
89-
await pageConditions.became(
90-
'typing indicator should not add to the live region',
91-
async () => () => liveRegionInnerTexts.join('\n') === 'Bot is typing.',
92-
5000
93-
);
94+
await waitFor(() => expect(liveRegionInnerTexts.join('\n')).toBe('Bot is typing.'), 5_000);
9495

9596
// THEN: The typing indicator should be shown.
9697
await pageConditions.typingIndicatorShown();
@@ -99,11 +100,7 @@
99100
clock.tick(5000);
100101

101102
// THEN: It should not add any live region elements.
102-
await pageConditions.became(
103-
'typing indicator should not add to the live region',
104-
async () => () => liveRegionInnerTexts.join('\n') === 'Bot is typing.',
105-
5000
106-
);
103+
await waitFor(() => expect(liveRegionInnerTexts.join('\n')).toBe('Bot is typing.'), 5_000);
107104

108105
// THEN: The typing indicator should be hidden.
109106
await pageConditions.typingIndicatorHidden();

__tests__/html2/hooks/private/renderHook.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ export default function renderHook(
5959
const render = ({ renderCallbackProps }) => {
6060
const element = document.querySelector('main');
6161

62-
ReactDOM.render(wrapUiIfNeeded(React.createElement(TestComponent, renderCallbackProps), renderOptions.wrapper), element);
62+
ReactDOM.render(
63+
wrapUiIfNeeded(React.createElement(TestComponent, { renderCallbackProps }), renderOptions.wrapper),
64+
element
65+
);
6366

6467
return { rerender: render, unmount: () => ReactDOM.unmountComponentAtNode(element) };
6568
};
@@ -70,7 +73,7 @@ export default function renderHook(
7073
);
7174

7275
function rerender(rerenderCallbackProps) {
73-
return baseRerender(React.createElement(TestComponent, { renderCallbackProps: rerenderCallbackProps }));
76+
return baseRerender({ renderCallbackProps: rerenderCallbackProps });
7477
}
7578

7679
return { result, rerender, unmount };
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
5+
<script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
6+
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
7+
<script crossorigin="anonymous" src="/test-harness.js"></script>
8+
<script crossorigin="anonymous" src="/test-page-object.js"></script>
9+
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
10+
</head>
11+
<body>
12+
<main id="webchat"></main>
13+
<script type="importmap">
14+
{
15+
"imports": {
16+
"@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs",
17+
"jest-mock": "https://esm.sh/jest-mock"
18+
}
19+
}
20+
</script>
21+
<script type="module">
22+
import { waitFor } from '@testduet/wait-for';
23+
import { fn, spyOn } from 'jest-mock';
24+
import renderHook from './private/renderHook.js';
25+
26+
const {
27+
React: { createElement },
28+
testHelpers: { createDirectLineEmulator },
29+
WebChat: {
30+
Components: { BasicWebChat, Composer },
31+
hooks: { useEmitTypingIndicator }
32+
}
33+
} = window;
34+
35+
run(async function () {
36+
const { directLine, store } = createDirectLineEmulator();
37+
const WebChatWrapper = ({ children }) =>
38+
createElement(Composer, { directLine, store }, createElement(BasicWebChat), children);
39+
40+
// WHEN: Render initially.
41+
const renderResult = renderHook(
42+
({ run } = {}) => {
43+
const emitTypingIndicator = useEmitTypingIndicator();
44+
45+
run && emitTypingIndicator();
46+
},
47+
{ legacyRoot: true, wrapper: WebChatWrapper }
48+
);
49+
50+
await pageConditions.uiConnected();
51+
52+
// WHEN: The callback function from the useEmitTypingIndicator() hook is being called.
53+
const result = await directLine.actPostActivity(() => renderResult.rerender({ run: true }));
54+
55+
// THEN: Should send a "typing" activity.
56+
expect(result.activity).toHaveProperty('type', 'typing');
57+
58+
// THEN: Should originate from the user.
59+
expect(result.activity).toHaveProperty('from.role', 'user');
60+
});
61+
</script>
62+
</body>
63+
</html>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
5+
<script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
6+
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
7+
<script crossorigin="anonymous" src="/test-harness.js"></script>
8+
<script crossorigin="anonymous" src="/test-page-object.js"></script>
9+
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
10+
</head>
11+
<body>
12+
<main id="webchat"></main>
13+
<script type="importmap">
14+
{
15+
"imports": {
16+
"@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs",
17+
"jest-mock": "https://esm.sh/jest-mock"
18+
}
19+
}
20+
</script>
21+
<script type="module">
22+
import { waitFor } from '@testduet/wait-for';
23+
import { fn, spyOn } from 'jest-mock';
24+
import renderHook from './private/renderHook.js';
25+
26+
const {
27+
React: { createElement },
28+
testHelpers: { createDirectLineEmulator },
29+
WebChat: {
30+
Components: { BasicWebChat, Composer },
31+
hooks: { useSendTypingIndicator }
32+
}
33+
} = window;
34+
35+
run(async function () {
36+
const { directLine, store } = createDirectLineEmulator();
37+
const WebChatWrapper = ({ children }) =>
38+
createElement(
39+
Composer,
40+
{ directLine, sendTypingIndicator: true, store },
41+
createElement(BasicWebChat),
42+
children
43+
);
44+
45+
// WHEN: The state hook is being called.
46+
const renderResult = renderHook(() => useSendTypingIndicator(), { legacyRoot: true, wrapper: WebChatWrapper });
47+
48+
// THEN: Should return state as true.
49+
expect(renderResult).toHaveProperty('result.current', [true]);
50+
});
51+
</script>
52+
</body>
53+
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
5+
<script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
6+
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
7+
<script crossorigin="anonymous" src="/test-harness.js"></script>
8+
<script crossorigin="anonymous" src="/test-page-object.js"></script>
9+
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
10+
</head>
11+
<body>
12+
<main id="webchat"></main>
13+
<script type="importmap">
14+
{
15+
"imports": {
16+
"@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs",
17+
"jest-mock": "https://esm.sh/jest-mock"
18+
}
19+
}
20+
</script>
21+
<script type="module">
22+
import { waitFor } from '@testduet/wait-for';
23+
import { fn, spyOn } from 'jest-mock';
24+
import renderHook from './private/renderHook.js';
25+
26+
const {
27+
React: { createElement },
28+
testHelpers: { createDirectLineEmulator },
29+
WebChat: {
30+
Components: { BasicWebChat, Composer },
31+
hooks: { useSendTypingIndicator }
32+
}
33+
} = window;
34+
35+
run(async function () {
36+
const { directLine, store } = createDirectLineEmulator();
37+
const WebChatWrapper = ({ children }) =>
38+
createElement(Composer, { directLine, store }, createElement(BasicWebChat), children);
39+
40+
// WHEN: The state hook is being called.
41+
const renderResult = renderHook(() => useSendTypingIndicator(), { legacyRoot: true, wrapper: WebChatWrapper });
42+
43+
// THEN: Should return state as true.
44+
expect(renderResult).toHaveProperty('result.current', [false]);
45+
});
46+
</script>
47+
</body>
48+
</html>

packages/api/src/hooks/private/reduceIterable.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable no-magic-numbers */
2-
31
import reduceIterable from './reduceIterable';
42

53
describe('when called with a summation reducer', () => {

0 commit comments

Comments
 (0)