Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { render, fireEvent } from "@testing-library/react";
import { Provider } from "mobx-react";
import { SkipButton } from "../buttons";
 
jest.mock("@humansignal/ui", () => {
  const { forwardRef } = jest.requireActual("react");
  return {
    Button: forwardRef(({ children, disabled, tooltip, onClick, ...props }: any, ref: any) => {
      return (
        <button {...props} ref={ref} data-testid="skip-button" disabled={disabled} title={tooltip} onClick={onClick}>
          {children}
        </button>
      );
    }),
    Tooltip: ({ children, title }: any) => {
      return (
        <div data-testid="tooltip" title={title}>
          {children}
        </div>
      );
    },
  };
});
 
jest.mock("@humansignal/icons", () => ({
  IconInfoOutline: ({ width, height, className }: any) => (
    <svg data-testid="info-icon" width={width} height={height} className={className} />
  ),
}));
 
const createMockStore = (overrides: any = {}) => ({
  task: { id: 1, allow_skip: true, ...overrides.task },
  skipTask: jest.fn(),
  hasInterface: jest.fn((name: string) => overrides.interfaces?.includes(name) ?? false),
  annotationStore: {
    selected: {
      submissionInProgress: jest.fn(),
    },
  },
  commentStore: {
    commentFormSubmit: jest.fn(),
  },
  ...overrides,
});
 
// Helper to set up window.APP_SETTINGS for role-based tests
const setupAppSettings = (role?: string) => {
  (window as any).APP_SETTINGS = {
    user: {
      role,
    },
  };
};
 
describe("SkipButton", () => {
  beforeEach(() => {
    jest.clearAllMocks();
    // Reset APP_SETTINGS before each test
    (window as any).APP_SETTINGS = undefined;
  });
 
  test("Skip button disabled when allow_skip=false", () => {
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).toBeDisabled();
    expect(button).toHaveAttribute("title", "This task cannot be skipped");
  });
 
  test("Skip button enabled when allow_skip=true", () => {
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: true },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
  });
 
  test("Skip button enabled when allow_skip is undefined (default behavior)", () => {
    const mockStore = createMockStore({
      task: { id: 1 }, // no allow_skip property
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
  });
 
  test("Skip button enabled when allow_skip=null", () => {
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: null },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
  });
 
  test("Skip button onClick doesn't trigger when allow_skip=false", () => {
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
      interfaces: ["skip"],
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    fireEvent.click(button);
 
    expect(onSkipWithComment).not.toHaveBeenCalled();
    expect(mockStore.skipTask).not.toHaveBeenCalled();
  });
 
  test("Skip button onClick triggers when allow_skip=true", async () => {
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: true },
      interfaces: ["skip", "comments:skip"],
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    fireEvent.click(button);
 
    expect(onSkipWithComment).toHaveBeenCalled();
  });
 
  test("Skip button respects other disabled conditions", () => {
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: true },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={true} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).toBeDisabled();
  });
 
  // Role-based tests (OW=Owner, AD=Admin, MA=Manager can force-skip)
  test("Skip button enabled when allow_skip=false but user is Owner (OW)", () => {
    setupAppSettings("OW");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
    // Check that info icon is shown for managers when task is unskippable
    expect(getByTestId("info-icon")).toBeInTheDocument();
  });
 
  test("Skip button enabled when allow_skip=false but user is Admin (AD)", () => {
    setupAppSettings("AD");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
    // Check that info icon is shown for managers when task is unskippable
    expect(getByTestId("info-icon")).toBeInTheDocument();
  });
 
  test("Skip button enabled when allow_skip=false but user is Manager (MA)", () => {
    setupAppSettings("MA");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
    // Check that info icon is shown for managers when task is unskippable
    expect(getByTestId("info-icon")).toBeInTheDocument();
  });
 
  test("Skip button disabled when allow_skip=false and user is Annotator (AN)", () => {
    setupAppSettings("AN");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).toBeDisabled();
    expect(button).toHaveAttribute("title", "This task cannot be skipped");
  });
 
  test("Skip button disabled when allow_skip=false and user is Reviewer (RE)", () => {
    setupAppSettings("RE");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).toBeDisabled();
    expect(button).toHaveAttribute("title", "This task cannot be skipped");
  });
 
  test("Skip button onClick triggers when allow_skip=false but user is Manager", () => {
    setupAppSettings("MA");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: false },
      interfaces: ["skip", "comments:skip"],
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    fireEvent.click(button);
 
    expect(onSkipWithComment).toHaveBeenCalled();
  });
 
  test("Skip button shows normal tooltip when allow_skip=true even for Manager", () => {
    setupAppSettings("MA");
    const mockStore = createMockStore({
      task: { id: 1, allow_skip: true },
    });
    const onSkipWithComment = jest.fn();
 
    const { getByTestId, queryByTestId } = render(
      <Provider store={mockStore}>
        <SkipButton disabled={false} store={mockStore as any} onSkipWithComment={onSkipWithComment} />
      </Provider>,
    );
 
    const button = getByTestId("skip-button");
    expect(button).not.toBeDisabled();
    // When task allows skip, show normal tooltip even if user is manager
    expect(button).toHaveAttribute("title", "Cancel (skip) task [ Ctrl+Space ]");
    // Info icon should not be shown when task allows skip
    expect(queryByTestId("info-icon")).not.toBeInTheDocument();
  });
});