• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fkhadra / react-toastify / 12001201729

25 Nov 2024 12:54AM UTC coverage: 42.537% (-46.5%) from 88.998%
12001201729

Pull #1178

github

fkhadra
bump coverall action
Pull Request #1178: [WIP] V11

141 of 375 branches covered (37.6%)

Branch coverage included in aggregate %.

3 of 5 new or added lines in 3 files covered. (60.0%)

214 existing lines in 9 files now uncovered.

201 of 429 relevant lines covered (46.85%)

176.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.1
/src/core/store.ts
1
import {
2
  ClearWaitingQueueParams,
3
  Id,
4
  NotValidatedToastProps,
5
  OnChangeCallback,
6
  ToastContainerProps,
7
  ToastContent,
8
  ToastItem,
9
  ToastOptions
10
} from '../types';
11
import { Default, canBeRendered, isId } from '../utils';
12
import {
13
  ContainerObserver,
14
  createContainerObserver
15
} from './containerObserver';
16

17
interface EnqueuedToast {
18
  content: ToastContent<any>;
19
  options: NotValidatedToastProps;
20
}
21

22
interface RemoveParams {
23
  id?: Id;
24
  containerId: Id;
25
}
26

27
const containers = new Map<Id, ContainerObserver>();
46✔
28
let renderQueue: EnqueuedToast[] = [];
46✔
29
const listeners = new Set<OnChangeCallback>();
46✔
30

31
const dispatchChanges = (data: ToastItem) => listeners.forEach(cb => cb(data));
46✔
32

33
const hasContainers = () => containers.size > 0;
46✔
34

35
function flushRenderQueue() {
UNCOV
36
  renderQueue.forEach(v => pushToast(v.content, v.options));
×
UNCOV
37
  renderQueue = [];
×
38
}
39

40
export const getToast = (id: Id, { containerId }: ToastOptions) =>
46✔
UNCOV
41
  containers.get(containerId || Default.CONTAINER_ID)?.toasts.get(id);
×
42

43
export function isToastActive(id: Id, containerId?: Id) {
UNCOV
44
  if (containerId) return !!containers.get(containerId)?.isToastActive(id);
×
45

UNCOV
46
  let isActive = false;
×
UNCOV
47
  containers.forEach(c => {
×
UNCOV
48
    if (c.isToastActive(id)) isActive = true;
×
49
  });
50

UNCOV
51
  return isActive;
×
52
}
53

54
export function removeToast(params?: Id | RemoveParams) {
UNCOV
55
  if (!hasContainers()) {
×
UNCOV
56
    renderQueue = renderQueue.filter(
×
UNCOV
57
      v => params != null && v.options.toastId !== params
×
58
    );
UNCOV
59
    return;
×
60
  }
61

UNCOV
62
  if (params == null || isId(params)) {
×
UNCOV
63
    containers.forEach(c => {
×
UNCOV
64
      c.removeToast(params as Id);
×
65
    });
UNCOV
66
  } else if (params && ('containerId' in params || 'id' in params)) {
×
UNCOV
67
    const container = containers.get(params.containerId);
×
UNCOV
68
    container
×
69
      ? container.removeToast(params.id)
70
      : containers.forEach(c => {
UNCOV
71
          c.removeToast(params.id);
×
72
        });
73
  }
74
}
75

76
export const clearWaitingQueue = (p: ClearWaitingQueueParams = {}) => {
46!
UNCOV
77
  containers.forEach(c => {
×
UNCOV
78
    if (c.props.limit && (!p.containerId || c.id === p.containerId)) {
×
UNCOV
79
      c.clearQueue();
×
80
    }
81
  });
82
};
83

84
export function pushToast<TData>(
85
  content: ToastContent<TData>,
86
  options: NotValidatedToastProps
87
) {
UNCOV
88
  if (!canBeRendered(content)) return;
×
UNCOV
89
  if (!hasContainers()) renderQueue.push({ content, options });
×
90

UNCOV
91
  containers.forEach(c => {
×
UNCOV
92
    c.buildToast(content, options);
×
93
  });
94
}
95

96
interface ToggleToastParams {
97
  id?: Id;
98
  containerId?: Id;
99
}
100

101
type RegisterToggleOpts = {
102
  id: Id;
103
  containerId?: Id;
104
  fn: (v: boolean) => void;
105
};
106

107
export function registerToggle(opts: RegisterToggleOpts) {
108
  containers
1,030✔
109
    .get(opts.containerId || Default.CONTAINER_ID)
1,030✔
110
    ?.setToggle(opts.id, opts.fn);
111
}
112

113
export function toggleToast(v: boolean, opt?: ToggleToastParams) {
UNCOV
114
  containers.forEach(c => {
×
UNCOV
115
    if (opt == null || !opt?.containerId) {
×
UNCOV
116
      c.toggle(v, opt?.id);
×
117
    } else if (opt?.containerId === c.id) {
×
118
      c.toggle(v, opt?.id);
×
119
    }
120
  });
121
}
122

123
export function registerContainer(props: ToastContainerProps) {
UNCOV
124
  const id = props.containerId || Default.CONTAINER_ID;
×
UNCOV
125
  return {
×
126
    subscribe(notify: () => void) {
UNCOV
127
      const container = createContainerObserver(id, props, dispatchChanges);
×
128

UNCOV
129
      containers.set(id, container);
×
UNCOV
130
      const unobserve = container.observe(notify);
×
UNCOV
131
      flushRenderQueue();
×
132

UNCOV
133
      return () => {
×
UNCOV
134
        unobserve();
×
UNCOV
135
        containers.delete(id);
×
136
      };
137
    },
138
    setProps(p: ToastContainerProps) {
UNCOV
139
      containers.get(id)?.setProps(p);
×
140
    },
141
    getSnapshot() {
UNCOV
142
      return containers.get(id)?.getSnapshot();
×
143
    }
144
  };
145
}
146

147
export function onChange(cb: OnChangeCallback) {
UNCOV
148
  listeners.add(cb);
×
149

UNCOV
150
  return () => {
×
UNCOV
151
    listeners.delete(cb);
×
152
  };
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc