cozy-bar 35.3.2 → 35.4.0

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.
@@ -9,16 +9,22 @@ exports.useBanners = void 0;
9
9
 
10
10
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
11
 
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
+
12
14
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
15
 
14
16
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
17
 
18
+ var _logger = _interopRequireDefault(require("../../lib/logger"));
19
+
16
20
  var _react = require("react");
17
21
 
18
22
  var _cozyClient = require("cozy-client");
19
23
 
20
24
  var _banner = require("cozy-client/dist/models/banner");
21
25
 
26
+ var _useRealtime2 = _interopRequireDefault(require("cozy-realtime/dist/useRealtime"));
27
+
22
28
  var POLL_INTERVAL_MS = 5 * 60 * 1000;
23
29
  /**
24
30
  * The banners to display, kept up to date. cozy-client owns the reading rules;
@@ -64,8 +70,8 @@ var useBanners = function useBanners() {
64
70
  case 10:
65
71
  _context.prev = 10;
66
72
  _context.t0 = _context["catch"](3);
67
- // eslint-disable-next-line no-console
68
- console.warn('[cozy-bar] could not read the platform banners', _context.t0);
73
+
74
+ _logger.default.warn('could not read the platform banners', _context.t0);
69
75
 
70
76
  case 13:
71
77
  case "end":
@@ -74,6 +80,11 @@ var useBanners = function useBanners() {
74
80
  }
75
81
  }, _callee, null, [[3, 10]]);
76
82
  })), [client]);
83
+ (0, _useRealtime2.default)(client !== null && client !== void 0 ? client : {}, (0, _defineProperty2.default)({}, _banner.BANNERS_DOCTYPE, {
84
+ created: read,
85
+ updated: read,
86
+ deleted: read
87
+ }), [read]);
77
88
  (0, _react.useEffect)(function () {
78
89
  var readIfVisible = function readIfVisible() {
79
90
  if (document.visibilityState !== 'visible') return;
@@ -114,8 +125,9 @@ var useBanners = function useBanners() {
114
125
  case 7:
115
126
  _context2.prev = 7;
116
127
  _context2.t0 = _context2["catch"](2);
117
- // eslint-disable-next-line no-console
118
- console.warn('[cozy-bar] could not record the dismissal', _context2.t0);
128
+
129
+ _logger.default.warn('could not record the dismissal', _context2.t0);
130
+
119
131
  read();
120
132
 
121
133
  case 11:
@@ -0,0 +1,87 @@
1
+ import { render, waitFor } from '@testing-library/react'
2
+ import React from 'react'
3
+
4
+ import { useBanners } from './useBanners'
5
+
6
+ jest.mock('cozy-client', () => ({ useClient: jest.fn() }))
7
+ jest.mock('cozy-client/dist/models/banner', () => ({
8
+ BANNERS_DOCTYPE: 'io.cozy.banners',
9
+ getActiveBanners: jest.fn(),
10
+ dismiss: jest.fn()
11
+ }))
12
+
13
+ const { useClient } = require('cozy-client')
14
+ const { getActiveBanners } = require('cozy-client/dist/models/banner')
15
+
16
+ const Probe = () => {
17
+ const { banners } = useBanners()
18
+ return <div data-testid="count">{banners.length}</div>
19
+ }
20
+
21
+ const setup = realtime => {
22
+ useClient.mockReturnValue({ plugins: realtime ? { realtime } : {} })
23
+ return render(<Probe />)
24
+ }
25
+
26
+ describe('useBanners', () => {
27
+ beforeEach(() => {
28
+ jest.clearAllMocks()
29
+ getActiveBanners.mockResolvedValue([])
30
+ })
31
+
32
+ it('subscribes to the banner events and drops them on unmount', async () => {
33
+ const realtime = { subscribe: jest.fn(), unsubscribe: jest.fn() }
34
+ const { unmount } = setup(realtime)
35
+
36
+ await waitFor(() => expect(realtime.subscribe).toHaveBeenCalledTimes(3))
37
+ expect(realtime.subscribe.mock.calls.map(([event]) => event)).toEqual([
38
+ 'created',
39
+ 'updated',
40
+ 'deleted'
41
+ ])
42
+ expect(realtime.subscribe).toHaveBeenCalledWith(
43
+ 'created',
44
+ 'io.cozy.banners',
45
+ expect.any(Function)
46
+ )
47
+
48
+ unmount()
49
+ expect(realtime.unsubscribe.mock.calls).toEqual(
50
+ realtime.subscribe.mock.calls
51
+ )
52
+ })
53
+
54
+ it('re-reads when a realtime event arrives', async () => {
55
+ const realtime = { subscribe: jest.fn(), unsubscribe: jest.fn() }
56
+ const { findByTestId, getByTestId } = setup(realtime)
57
+
58
+ await findByTestId('count')
59
+ expect(getActiveBanners).toHaveBeenCalledTimes(1)
60
+
61
+ getActiveBanners.mockResolvedValue([{ bannerId: 'demo' }])
62
+ const [, , handler] = realtime.subscribe.mock.calls[1]
63
+ handler()
64
+
65
+ await waitFor(() => expect(getByTestId('count')).toHaveTextContent('1'))
66
+ })
67
+
68
+ it('still renders without the realtime plugin', async () => {
69
+ const { findByTestId } = setup(null)
70
+ await expect(findByTestId('count')).resolves.toHaveTextContent('0')
71
+ })
72
+
73
+ it('renders when the realtime plugin is registered but logged out', async () => {
74
+ const loggedOut = () => {
75
+ throw new Error(
76
+ 'Unable to use realtime while cozy-client is not logged in'
77
+ )
78
+ }
79
+ const { findByTestId, unmount } = setup({
80
+ subscribe: loggedOut,
81
+ unsubscribe: loggedOut
82
+ })
83
+
84
+ await expect(findByTestId('count')).resolves.toHaveTextContent('0')
85
+ expect(() => unmount()).not.toThrow()
86
+ })
87
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-bar",
3
- "version": "35.3.2",
3
+ "version": "35.4.0",
4
4
  "description": "cozy-bar.js library, a small lib provided by cozy-stack to inject the Cozy-bar component into each app",
5
5
  "main": "dist/index.js",
6
6
  "author": "Cozy Cloud <contact@cozycloud.cc> (https://cozy.io/)",
@@ -75,5 +75,5 @@
75
75
  "react-router-dom": ">=6.14.2",
76
76
  "twake-i18n": ">=0.3.0"
77
77
  },
78
- "gitHead": "aa6c91c03a8b734177fddb3075aa26110516c218"
78
+ "gitHead": "50ca3bc5c37f45ab07b1ca61fa8bcc4766d0a819"
79
79
  }