1// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5const {assert} = chai;
6
7import * as UI from '../../../../front_end/ui/ui.js';
8
9describe('ListModel', () => {
10  it('can be instantiated correctly without a list of items', () => {
11    const model = new UI.ListModel.ListModel();
12    assert.deepEqual([...model], []);
13  });
14
15  it('can be instantiated correctly with a list of items', () => {
16    const model = new UI.ListModel.ListModel([4, 5, 6]);
17    assert.deepEqual([...model], [4, 5, 6]);
18  });
19
20  it('supports replacing all list elements', () => {
21    const model = new UI.ListModel.ListModel();
22    model.replaceAll([0, 1, 2]);
23    assert.deepEqual([...model], [0, 1, 2]);
24  });
25
26  it('supports replacing a range of list elements', () => {
27    const model = new UI.ListModel.ListModel([0, 1, 2]);
28    model.replaceRange(0, 1, [5, 6, 7]);
29    assert.deepEqual([...model], [5, 6, 7, 1, 2]);
30  });
31
32  it('supports inserting new list elements', () => {
33    const model = new UI.ListModel.ListModel([5, 6, 7, 1, 2]);
34    model.insert(model.length, 10);
35    assert.deepEqual([...model], [5, 6, 7, 1, 2, 10]);
36  });
37
38  it('supports removing list elements', () => {
39    const model = new UI.ListModel.ListModel([5, 6, 7, 1, 2, 10]);
40    model.remove(model.length - 1);
41    assert.deepEqual([...model], [5, 6, 7, 1, 2]);
42  });
43
44  it('supports removing list elements', () => {
45    const model = new UI.ListModel.ListModel([5, 6, 7, 1, 2]);
46    model.remove(4);
47    assert.deepEqual([...model], [5, 6, 7, 1]);
48  });
49
50  it('supports replacing list elements in place', () => {
51    const model = new UI.ListModel.ListModel([5, 6, 7, 1]);
52    model.insert(1, 8);
53    assert.deepEqual([...model], [5, 8, 6, 7, 1]);
54  });
55
56  it('supports replacing list elements in place', () => {
57    const model = new UI.ListModel.ListModel([
58      0, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 4, 5, 6, 27, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 28, 29,
59    ]);
60    model.replaceRange(0, 29, []);
61    assert.deepEqual([...model], [29]);
62  });
63
64  it('fires an event when elements are replaced', () => {
65    const model = new UI.ListModel.ListModel([0, 1, 2]);
66    let eventData!: {index: number, removed: number[], inserted: number};
67    model.addEventListener(UI.ListModel.Events.ItemsReplaced, (event: {data: typeof eventData}) => {
68      eventData = event.data;
69    });
70    model.replaceRange(0, 1, [5, 6, 7]);
71    assert.deepEqual([...model], [5, 6, 7, 1, 2]);
72    assert.deepEqual(eventData, {
73      index: 0,
74      removed: [0],
75      inserted: 3,
76    });
77  });
78});
79