@untemps/react-vocal 1.7.35 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.github/workflows/publish.yml +2 -0
  2. package/.husky/commit-msg +1 -0
  3. package/.husky/pre-commit +1 -0
  4. package/.prettierignore +1 -0
  5. package/.prettierrc +1 -1
  6. package/CHANGELOG.md +29 -0
  7. package/CLAUDE.md +12 -8
  8. package/README.md +23 -16
  9. package/dev/index.html +24 -0
  10. package/dev/package.json +12 -8
  11. package/dev/src/{index.js → index.jsx} +2 -3
  12. package/dev/vite.config.js +10 -0
  13. package/dev/yarn.lock +296 -172
  14. package/dist/index.es.js +2182 -2
  15. package/dist/index.es.js.map +1 -0
  16. package/dist/index.js +9 -2
  17. package/dist/index.js.map +1 -0
  18. package/dist/index.umd.js +9 -2
  19. package/dist/index.umd.js.map +1 -0
  20. package/package.json +27 -60
  21. package/src/components/{Icon.js → Icon.jsx} +1 -14
  22. package/src/components/Vocal.jsx +206 -0
  23. package/src/components/__tests__/{Icon.test.js → Icon.test.jsx} +0 -4
  24. package/src/components/__tests__/{Vocal.test.js → Vocal.test.jsx} +175 -18
  25. package/src/components/__tests__/{VocalWithMockedUseVocal.test.js → VocalWithMockedUseVocal.test.jsx} +11 -13
  26. package/src/components/__tests__/__snapshots__/Icon.test.jsx.snap +21 -0
  27. package/src/components/__tests__/__snapshots__/Vocal.test.jsx.snap +28 -0
  28. package/src/hooks/__tests__/useCommands.test.js +1 -1
  29. package/src/hooks/__tests__/useTimeout.test.js +6 -6
  30. package/src/hooks/__tests__/useVocal.test.js +15 -15
  31. package/src/hooks/useTimeout.js +0 -2
  32. package/src/hooks/useVocal.js +0 -2
  33. package/vite.config.js +35 -0
  34. package/vitest.setup.js +74 -0
  35. package/babel.config.js +0 -12
  36. package/dev/babel.config.js +0 -4
  37. package/dev/rollup.config.js +0 -29
  38. package/jest/jest.setup.js +0 -72
  39. package/rollup.config.js +0 -42
  40. package/src/components/Vocal.js +0 -235
@@ -0,0 +1,21 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`Icon > matches snapshot 1`] = `
4
+ <DocumentFragment>
5
+ <svg
6
+ data-testid="__icon-root__"
7
+ height="100%"
8
+ viewBox="0 0 24 24"
9
+ width="100%"
10
+ xmlns="http://www.w3.org/2000/svg"
11
+ >
12
+ <g>
13
+ <path
14
+ d="M12 14c1.66 0 2.99-1.34 2.99-3L15 5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm5.3-3c0 3-2.54 5.1-5.3 5.1S6.7 14 6.7 11H5c0 3.41 2.72 6.23 6 6.72V21h2v-3.28c3.28-.48 6-3.3 6-6.72h-1.7z"
15
+ data-testid="__icon-path__"
16
+ fill="black"
17
+ />
18
+ </g>
19
+ </svg>
20
+ </DocumentFragment>
21
+ `;
@@ -0,0 +1,28 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`Vocal > matches snapshot 1`] = `
4
+ <DocumentFragment>
5
+ <button
6
+ aria-label="start recognition"
7
+ data-testid="__vocal-root__"
8
+ role="button"
9
+ style="width: 24px; height: 24px; background-color: transparent; border: medium; padding: 0px; cursor: pointer;"
10
+ >
11
+ <svg
12
+ data-testid="__icon-root__"
13
+ height="100%"
14
+ viewBox="0 0 24 24"
15
+ width="100%"
16
+ xmlns="http://www.w3.org/2000/svg"
17
+ >
18
+ <g>
19
+ <path
20
+ d="M12 14c1.66 0 2.99-1.34 2.99-3L15 5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm5.3-3c0 3-2.54 5.1-5.3 5.1S6.7 14 6.7 11H5c0 3.41 2.72 6.23 6 6.72V21h2v-3.28c3.28-.48 6-3.3 6-6.72h-1.7z"
21
+ data-testid="__icon-path__"
22
+ fill="#aaa"
23
+ />
24
+ </g>
25
+ </svg>
26
+ </button>
27
+ </DocumentFragment>
28
+ `;
@@ -1,4 +1,4 @@
1
- import { renderHook } from '@testing-library/react-hooks'
1
+ import { renderHook } from '@testing-library/react'
2
2
 
3
3
  import useCommands from '../useCommands'
4
4
 
@@ -1,4 +1,4 @@
1
- import { renderHook } from '@testing-library/react-hooks'
1
+ import { renderHook } from '@testing-library/react'
2
2
 
3
3
  import useTimeout from '../useTimeout'
4
4
 
@@ -10,13 +10,13 @@ const wait = (delay) => {
10
10
 
11
11
  describe('useTimeout', () => {
12
12
  it('not triggers handler before calling start', () => {
13
- const handler = jest.fn()
13
+ const handler = vi.fn()
14
14
  renderHook(() => useTimeout(handler))
15
15
  expect(handler).not.toHaveBeenCalled()
16
16
  })
17
17
 
18
18
  it('not triggers handler before timeout', async () => {
19
- const handler = jest.fn()
19
+ const handler = vi.fn()
20
20
  const timeout = 500
21
21
  const {
22
22
  result: {
@@ -29,7 +29,7 @@ describe('useTimeout', () => {
29
29
  })
30
30
 
31
31
  it('triggers handler immediately', async () => {
32
- const handler = jest.fn()
32
+ const handler = vi.fn()
33
33
  const {
34
34
  result: {
35
35
  current: [start],
@@ -41,7 +41,7 @@ describe('useTimeout', () => {
41
41
  })
42
42
 
43
43
  it('triggers handler after delay', async () => {
44
- const handler = jest.fn()
44
+ const handler = vi.fn()
45
45
  const timeout = 500
46
46
  const {
47
47
  result: {
@@ -54,7 +54,7 @@ describe('useTimeout', () => {
54
54
  })
55
55
 
56
56
  it('not triggers handler if stop is called before timeout', async () => {
57
- const handler = jest.fn()
57
+ const handler = vi.fn()
58
58
  const timeout = 500
59
59
  const {
60
60
  result: {
@@ -1,19 +1,19 @@
1
- import { renderHook } from '@testing-library/react-hooks'
1
+ import { renderHook } from '@testing-library/react'
2
2
  import { Vocal as SpeechRecognitionWrapper } from '@untemps/vocal'
3
3
 
4
4
  import useVocal from '../useVocal'
5
5
 
6
- jest.mock('@untemps/vocal')
6
+ vi.mock('@untemps/vocal')
7
7
 
8
8
  describe('useVocal', () => {
9
- const mockStart = jest.fn()
10
- const mockStop = jest.fn()
11
- const mockAbort = jest.fn()
12
- const mockAddEventListener = jest.fn()
13
- const mockRemoveEventListener = jest.fn()
14
- const mockCleanup = jest.fn()
15
-
16
- const mockIsSupported = jest.fn()
9
+ const mockStart = vi.fn()
10
+ const mockStop = vi.fn()
11
+ const mockAbort = vi.fn()
12
+ const mockAddEventListener = vi.fn()
13
+ const mockRemoveEventListener = vi.fn()
14
+ const mockCleanup = vi.fn()
15
+
16
+ const mockIsSupported = vi.fn()
17
17
  Object.defineProperty(SpeechRecognitionWrapper, 'isSupported', {
18
18
  get: mockIsSupported,
19
19
  })
@@ -78,7 +78,7 @@ describe('useVocal', () => {
78
78
  current: [, { subscribe }],
79
79
  },
80
80
  } = renderHook(() => useVocal())
81
- subscribe('foo', jest.fn())
81
+ subscribe('foo', vi.fn())
82
82
  expect(mockAddEventListener).not.toHaveBeenCalled()
83
83
  })
84
84
 
@@ -88,7 +88,7 @@ describe('useVocal', () => {
88
88
  current: [, { unsubscribe }],
89
89
  },
90
90
  } = renderHook(() => useVocal())
91
- unsubscribe('foo', jest.fn())
91
+ unsubscribe('foo', vi.fn())
92
92
  expect(mockRemoveEventListener).not.toHaveBeenCalled()
93
93
  })
94
94
  })
@@ -99,7 +99,7 @@ describe('useVocal', () => {
99
99
  })
100
100
 
101
101
  beforeEach(() => {
102
- SpeechRecognitionWrapper.mockImplementation(() => {
102
+ SpeechRecognitionWrapper.mockImplementation(function () {
103
103
  return {
104
104
  start: mockStart,
105
105
  stop: mockStop,
@@ -180,7 +180,7 @@ describe('useVocal', () => {
180
180
  current: [, { subscribe }],
181
181
  },
182
182
  } = renderHook(() => useVocal())
183
- subscribe('foo', jest.fn())
183
+ subscribe('foo', vi.fn())
184
184
  expect(mockAddEventListener).toHaveBeenCalled()
185
185
  })
186
186
 
@@ -190,7 +190,7 @@ describe('useVocal', () => {
190
190
  current: [, { unsubscribe }],
191
191
  },
192
192
  } = renderHook(() => useVocal())
193
- unsubscribe('foo', jest.fn())
193
+ unsubscribe('foo', vi.fn())
194
194
  expect(mockRemoveEventListener).toHaveBeenCalled()
195
195
  })
196
196
  })
@@ -19,5 +19,3 @@ const useTimeout = (handler, timeout = 0) => {
19
19
  }
20
20
 
21
21
  export default useTimeout
22
-
23
- // TODO: Return a promise
@@ -54,5 +54,3 @@ const useVocal = (lang = 'en-US', grammars = null, __rsInstance = null) => {
54
54
  }
55
55
 
56
56
  export default useVocal
57
-
58
- // TODO: Return the instance, not the ref
package/vite.config.js ADDED
@@ -0,0 +1,35 @@
1
+ import { defineConfig } from 'vite'
2
+ import react from '@vitejs/plugin-react'
3
+
4
+ export default defineConfig({
5
+ plugins: [react()],
6
+ build: {
7
+ lib: {
8
+ entry: 'src/index.js',
9
+ name: 'ReactVocal',
10
+ formats: ['es', 'cjs', 'umd'],
11
+ fileName: (format) => ({ es: 'index.es.js', umd: 'index.umd.js', cjs: 'index.js' })[format],
12
+ },
13
+ rollupOptions: {
14
+ external: ['react', 'react-dom'],
15
+ output: {
16
+ globals: {
17
+ react: 'React',
18
+ 'react-dom': 'ReactDOM',
19
+ },
20
+ },
21
+ },
22
+ sourcemap: true,
23
+ },
24
+ test: {
25
+ globals: true,
26
+ environment: 'jsdom',
27
+ setupFiles: ['./vitest.setup.js'],
28
+ restoreMocks: true,
29
+ coverage: {
30
+ provider: 'v8',
31
+ reporter: ['text', 'lcov'],
32
+ reportsDirectory: './coverage',
33
+ },
34
+ },
35
+ })
@@ -0,0 +1,74 @@
1
+ import { vi } from 'vitest'
2
+ import '@testing-library/jest-dom/vitest'
3
+
4
+ Object.defineProperty(global, 'navigator', {
5
+ value: { userAgent: 'node.js' },
6
+ writable: true,
7
+ configurable: true,
8
+ })
9
+ global.PermissionStatus = vi.fn(function () {
10
+ return {
11
+ state: 'granted',
12
+ addEventListener: vi.fn(),
13
+ }
14
+ })
15
+ global.Permissions = vi.fn(function () {
16
+ return {
17
+ query: vi.fn().mockResolvedValue(new PermissionStatus()),
18
+ }
19
+ })
20
+ Object.defineProperty(global.navigator, 'permissions', {
21
+ value: new Permissions(),
22
+ writable: true,
23
+ configurable: true,
24
+ })
25
+ global.MediaDevices = vi.fn(function () {
26
+ return {
27
+ getUserMedia: vi.fn().mockResolvedValue('foo'),
28
+ }
29
+ })
30
+ Object.defineProperty(global.navigator, 'mediaDevices', {
31
+ value: new MediaDevices(),
32
+ writable: true,
33
+ configurable: true,
34
+ })
35
+ global.SpeechGrammarList = vi.fn(function () {
36
+ return {
37
+ length: 0,
38
+ }
39
+ })
40
+ global.SpeechRecognition = vi.fn(function () {
41
+ const handlers = {}
42
+ return {
43
+ addEventListener: vi.fn(function (type, callback) {
44
+ handlers[type] = callback
45
+ }),
46
+ removeEventListener: vi.fn(),
47
+ dispatchEvent: vi.fn(),
48
+ start: vi.fn(function () {
49
+ handlers.start?.()
50
+ }),
51
+ stop: vi.fn(function () {
52
+ handlers.end?.()
53
+ }),
54
+ abort: vi.fn(function () {
55
+ handlers.end?.()
56
+ }),
57
+ say: vi.fn(function (sentence) {
58
+ handlers.speechstart?.()
59
+
60
+ const resultEvent = new Event('result')
61
+ resultEvent.resultIndex = 0
62
+ resultEvent.results = [[{ transcript: sentence }]]
63
+ handlers.speechend?.()
64
+ if (sentence) {
65
+ handlers.result?.(resultEvent)
66
+ } else {
67
+ handlers.nomatch?.()
68
+ }
69
+ }),
70
+ error: vi.fn(function (err) {
71
+ handlers.error?.(err)
72
+ }),
73
+ }
74
+ })
package/babel.config.js DELETED
@@ -1,12 +0,0 @@
1
- module.exports = api => ({
2
- "presets": [
3
- "@babel/preset-env",
4
- "@babel/preset-react"
5
- ],
6
- "plugins": [
7
- "@babel/plugin-proposal-class-properties",
8
- "@babel/plugin-transform-react-jsx",
9
- ...(api.env('test') ? ["@babel/plugin-transform-runtime"] : [])
10
- ]
11
- })
12
-
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- presets: ['@babel/preset-react'],
3
- plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-transform-react-jsx'],
4
- }
@@ -1,29 +0,0 @@
1
- import babel from '@rollup/plugin-babel'
2
- import resolve from '@rollup/plugin-node-resolve'
3
- import replace from '@rollup/plugin-replace'
4
- import serve from 'rollup-plugin-serve'
5
- import livereload from 'rollup-plugin-livereload'
6
- import commonjs from '@rollup/plugin-commonjs'
7
-
8
- export default {
9
- input: 'src/index.js',
10
- output: {
11
- file: 'dist/index.js',
12
- },
13
- plugins: [
14
- replace({
15
- 'process.env.NODE_ENV': JSON.stringify('development'),
16
- }),
17
- babel({
18
- exclude: 'node_modules/**',
19
- babelHelpers: 'bundled',
20
- }),
21
- resolve(),
22
- commonjs(),
23
- serve({
24
- open: true,
25
- contentBase: ['dist', 'public'],
26
- }),
27
- livereload(),
28
- ],
29
- }
@@ -1,72 +0,0 @@
1
- import '@testing-library/jest-dom/extend-expect'
2
- import { toBeInTheDocument, toHaveAttribute, toHaveStyle } from '@testing-library/jest-dom/matchers'
3
-
4
- expect.extend({ toBeInTheDocument, toHaveAttribute, toHaveStyle })
5
-
6
- Object.defineProperty(global, 'navigator', {
7
- value: { userAgent: 'node.js' },
8
- writable: true,
9
- configurable: true,
10
- })
11
- global.PermissionStatus = jest.fn(() => ({
12
- state: 'granted',
13
- addEventListener: jest.fn(),
14
- }))
15
- const status = new PermissionStatus()
16
- global.Permissions = jest.fn(() => ({
17
- query: jest.fn().mockResolvedValue(status),
18
- }))
19
- Object.defineProperty(global.navigator, 'permissions', {
20
- value: new Permissions(),
21
- writable: true,
22
- configurable: true,
23
- })
24
- global.MediaDevices = jest.fn(() => ({
25
- getUserMedia: jest.fn().mockResolvedValue('foo'),
26
- }))
27
- Object.defineProperty(global.navigator, 'mediaDevices', {
28
- value: new MediaDevices(),
29
- writable: true,
30
- configurable: true,
31
- })
32
- global.SpeechGrammarList = jest.fn(() => ({
33
- length: 0,
34
- }))
35
- global.SpeechRecognition = jest.fn(() => {
36
- const handlers = {}
37
- return {
38
- addEventListener: jest.fn((type, callback) => {
39
- handlers[type] = callback
40
- }),
41
- removeEventListener: jest.fn(),
42
- dispatchEvent: jest.fn(),
43
- start: jest.fn(() => {
44
- !!handlers.start && handlers.start()
45
- }),
46
- stop: jest.fn(() => {
47
- !!handlers.end && handlers.end()
48
- }),
49
- abort: jest.fn(() => {
50
- !!handlers.end && handlers.end()
51
- }),
52
- say: jest.fn((sentence) => {
53
- !!handlers.speechstart && handlers.speechstart()
54
-
55
- const resultEvent = new Event('result')
56
- resultEvent.resultIndex = 0
57
- resultEvent.results = [
58
- [
59
- {
60
- transcript: sentence,
61
- },
62
- ],
63
- ]
64
- if (sentence) {
65
- !!handlers.result && handlers.result(resultEvent)
66
- } else {
67
- !!handlers.nomatch && handlers.nomatch()
68
- }
69
- !!handlers.speechend && handlers.speechend()
70
- }),
71
- }
72
- })
package/rollup.config.js DELETED
@@ -1,42 +0,0 @@
1
- import babel from '@rollup/plugin-babel'
2
- import commonjs from '@rollup/plugin-commonjs'
3
- import resolve from '@rollup/plugin-node-resolve'
4
- import filesize from 'rollup-plugin-sizes'
5
- import { terser } from 'rollup-plugin-terser'
6
- import visualizer from 'rollup-plugin-visualizer'
7
-
8
- const production = process.env.NODE_ENV === 'production'
9
- const target = process.env.BABEL_ENV
10
-
11
- export default {
12
- input: 'src/index.js',
13
- output: {
14
- name: 'react-vocal',
15
- file: {
16
- cjs: 'dist/index.js',
17
- es: 'dist/index.es.js',
18
- umd: 'dist/index.umd.js',
19
- }[target],
20
- format: target,
21
- globals: {
22
- react: 'React',
23
- 'react-dom': 'ReactDOM',
24
- 'prop-types': 'PropTypes',
25
- },
26
- sourcemap: 'inline',
27
- },
28
- external: ['react', 'react-dom', 'prop-types', '@babel/plugin-transform-runtime'],
29
- plugins: [
30
- babel({
31
- exclude: 'node_modules/**',
32
- babelHelpers: 'bundled',
33
- }),
34
- resolve(),
35
- commonjs(),
36
- production && terser(),
37
- filesize(),
38
- visualizer({
39
- sourcemap: true
40
- })
41
- ],
42
- }