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

fkhadra / react-toastify / 12246748306

10 Dec 2024 12:09AM UTC coverage: 81.078% (-7.9%) from 88.998%
12246748306

Pull #1178

github

fkhadra
chore: remove scss reference
Pull Request #1178: [WIP] V11

313 of 414 branches covered (75.6%)

Branch coverage included in aggregate %.

55 of 63 new or added lines in 14 files covered. (87.3%)

38 existing lines in 6 files now uncovered.

424 of 495 relevant lines covered (85.66%)

563.04 hits per line

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

68.69
/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 { ContainerObserver, createContainerObserver } from './containerObserver';
13

14
interface EnqueuedToast {
15
  content: ToastContent<any>;
16
  options: NotValidatedToastProps;
17
}
18

19
interface RemoveParams {
20
  id?: Id;
21
  containerId: Id;
22
}
23

24
const containers = new Map<Id, ContainerObserver>();
86✔
25
let renderQueue: EnqueuedToast[] = [];
86✔
26
const listeners = new Set<OnChangeCallback>();
86✔
27

28
const dispatchChanges = (data: ToastItem) => listeners.forEach(cb => cb(data));
766✔
29

30
const hasContainers = () => containers.size > 0;
1,342✔
31

32
function flushRenderQueue() {
33
  renderQueue.forEach(v => pushToast(v.content, v.options));
484✔
34
  renderQueue = [];
418✔
35
}
36

37
export const getToast = (id: Id, { containerId }: ToastOptions) =>
86✔
38
  containers.get(containerId || Default.CONTAINER_ID)?.toasts.get(id);
138✔
39

40
export function isToastActive(id: Id, containerId?: Id) {
41
  if (containerId) return !!containers.get(containerId)?.isToastActive(id);
800!
42

43
  let isActive = false;
800✔
44
  containers.forEach(c => {
800✔
45
    if (c.isToastActive(id)) isActive = true;
800✔
46
  });
47

48
  return isActive;
800✔
49
}
50

51
export function removeToast(params?: Id | RemoveParams) {
52
  if (!hasContainers()) {
54✔
53
    renderQueue = renderQueue.filter(v => params != null && v.options.toastId !== params);
76✔
54
    return;
38✔
55
  }
56

57
  if (params == null || isId(params)) {
16!
58
    containers.forEach(c => {
16✔
59
      c.removeToast(params as Id);
16✔
60
    });
UNCOV
61
  } else if (params && ('containerId' in params || 'id' in params)) {
×
UNCOV
62
    const container = containers.get(params.containerId);
×
UNCOV
63
    container
×
64
      ? container.removeToast(params.id)
65
      : containers.forEach(c => {
UNCOV
66
          c.removeToast(params.id);
×
67
        });
68
  }
69
}
70

71
export const clearWaitingQueue = (p: ClearWaitingQueueParams = {}) => {
86!
UNCOV
72
  containers.forEach(c => {
×
UNCOV
73
    if (c.props.limit && (!p.containerId || c.id === p.containerId)) {
×
UNCOV
74
      c.clearQueue();
×
75
    }
76
  });
77
};
78

79
export function pushToast<TData>(content: ToastContent<TData>, options: NotValidatedToastProps) {
80
  if (!canBeRendered(content)) return;
1,288!
81
  if (!hasContainers()) renderQueue.push({ content, options });
1,288✔
82

83
  containers.forEach(c => {
1,288✔
84
    c.buildToast(content, options);
766✔
85
  });
86
}
87

88
interface ToggleToastParams {
89
  id?: Id;
90
  containerId?: Id;
91
}
92

93
type RegisterToggleOpts = {
94
  id: Id;
95
  containerId?: Id;
96
  fn: (v: boolean) => void;
97
};
98

99
export function registerToggle(opts: RegisterToggleOpts) {
100
  containers.get(opts.containerId || Default.CONTAINER_ID)?.setToggle(opts.id, opts.fn);
2,244✔
101
}
102

103
export function toggleToast(v: boolean, opt?: ToggleToastParams) {
104
  containers.forEach(c => {
28✔
105
    if (opt == null || !opt?.containerId) {
28!
106
      c.toggle(v, opt?.id);
28✔
107
    } else if (opt?.containerId === c.id) {
×
108
      c.toggle(v, opt?.id);
×
109
    }
110
  });
111
}
112

113
export function registerContainer(props: ToastContainerProps) {
114
  const id = props.containerId || Default.CONTAINER_ID;
1,134✔
115
  return {
1,134✔
116
    subscribe(notify: () => void) {
117
      const container = createContainerObserver(id, props, dispatchChanges);
418✔
118

119
      containers.set(id, container);
418✔
120
      const unobserve = container.observe(notify);
418✔
121
      flushRenderQueue();
418✔
122

123
      return () => {
418✔
124
        unobserve();
378✔
125
        containers.delete(id);
378✔
126
      };
127
    },
128
    setProps(p: ToastContainerProps) {
129
      containers.get(id)?.setProps(p);
1,134✔
130
    },
131
    getSnapshot() {
132
      return containers.get(id)?.getSnapshot();
4,184✔
133
    }
134
  };
135
}
136

137
export function onChange(cb: OnChangeCallback) {
138
  listeners.add(cb);
58✔
139

140
  return () => {
58✔
141
    listeners.delete(cb);
28✔
142
  };
143
}
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

© 2026 Coveralls, Inc