|
| 1 | +import { types } from 'mobx-state-tree'; |
| 2 | + |
| 3 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 4 | +// @ts-ignore |
| 5 | +import { ListModel } from '../../object/List'; |
| 6 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 7 | +// @ts-ignore |
| 8 | +import { RankerModel } from '../Ranker'; |
| 9 | + |
| 10 | +const MockAnnotationStore = types.model('Annotation', { |
| 11 | + names: types.map(types.union(RankerModel, ListModel)), |
| 12 | +}).volatile(() => ({ |
| 13 | + results: [] as any[], |
| 14 | +})); |
| 15 | + |
| 16 | +const MockStore = types |
| 17 | + .model({ |
| 18 | + annotationStore: types.model({ |
| 19 | + selected: MockAnnotationStore, |
| 20 | + }), |
| 21 | + }) |
| 22 | + .volatile(() => ({ |
| 23 | + task: { dataObj: {} }, |
| 24 | + })); |
| 25 | + |
| 26 | +const items = [ |
| 27 | + { 'id': 'item1', 'title': '111' }, |
| 28 | + { 'id': 'item2', 'title': '222' }, |
| 29 | + { 'id': 'item3', 'title': '333' }, |
| 30 | +]; |
| 31 | + |
| 32 | +describe('List + Ranker (rank mode)', () => { |
| 33 | + const list = ListModel.create({ name: 'list', value: '$items', title: 'Test List' }); |
| 34 | + const ranker = RankerModel.create({ name: 'rank', toname: 'list' }); |
| 35 | + const store = MockStore.create({ annotationStore: { selected: { names: { list, ranker } } } }); |
| 36 | + |
| 37 | + store.task.dataObj = { items }; |
| 38 | + list.updateValue(store); |
| 39 | + |
| 40 | + it('List and Ranker should get values from the task', () => { |
| 41 | + expect(list._value).toEqual(items); |
| 42 | + expect(Object.keys(ranker.list.items)).toEqual(['item1', 'item2', 'item3']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('Ranker should have proper columns and other getters', () => { |
| 46 | + expect(ranker.buckets).toEqual([]); |
| 47 | + expect(ranker.defaultBucket).toEqual('rank'); |
| 48 | + expect(ranker.rankOnly).toEqual(true); |
| 49 | + expect(ranker.columns).toEqual([{ id: 'rank', title: 'Test List' }]); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('List + Ranker + Buckets (pick mode)', () => { |
| 54 | + const list = ListModel.create({ name: 'list', value: '$items', title: 'Test List' }); |
| 55 | + const ranker = RankerModel.create({ name: 'rank', toname: 'list', children: [ |
| 56 | + { id: 'B1', type: 'bucket', name: 'B1', title: 'Bucket 1' }, |
| 57 | + { id: 'B2', type: 'bucket', name: 'B2', title: 'Bucket 2' }, |
| 58 | + ] }); |
| 59 | + const store = MockStore.create({ annotationStore: { selected: { names: { list, ranker } } } }); |
| 60 | + |
| 61 | + store.task.dataObj = { items }; |
| 62 | + list.updateValue(store); |
| 63 | + |
| 64 | + const columns = [ |
| 65 | + { id: '_', title: 'Test List' }, |
| 66 | + { id: 'B1', title: 'Bucket 1' }, |
| 67 | + { id: 'B2', title: 'Bucket 2' }, |
| 68 | + ]; |
| 69 | + const result: any = { |
| 70 | + from_name: ranker, |
| 71 | + to_name: list, |
| 72 | + type: 'ranker', |
| 73 | + value: { ranker: { B1: ['item2'] } }, |
| 74 | + }; |
| 75 | + |
| 76 | + it('List and Ranker should get values from the task', () => { |
| 77 | + expect(list._value).toEqual(items); |
| 78 | + expect(Object.keys(ranker.list.items)).toEqual(['item1', 'item2', 'item3']); |
| 79 | + }); |
| 80 | + |
| 81 | + it('Ranker should have proper columns and other getters', () => { |
| 82 | + expect(ranker.buckets.map((b: any) => b.name)).toEqual(['B1', 'B2']); |
| 83 | + expect(ranker.defaultBucket).toEqual(undefined); |
| 84 | + expect(ranker.rankOnly).toEqual(false); |
| 85 | + expect(ranker.columns).toEqual(columns); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Ranker puts all items into _ bucket if there is no result', () => { |
| 89 | + expect(ranker.dataSource).toEqual({ |
| 90 | + items: ranker.list.items, |
| 91 | + columns, |
| 92 | + itemIds: { _: ['item1', 'item2', 'item3'], B1: [], B2: [] }, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('Ranker returns items according to result and puts the rest to _ bucket', () => { |
| 97 | + store.annotationStore.selected.results.push(result); |
| 98 | + |
| 99 | + expect(ranker.result).toBeTruthy(); |
| 100 | + expect(ranker.dataSource).toEqual({ |
| 101 | + items: ranker.list.items, |
| 102 | + columns, |
| 103 | + itemIds: { B1: ['item2'], B2: [], _: ['item1', 'item3'] }, |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('List + Ranker + Buckets + default (group mode)', () => { |
| 109 | + const list = ListModel.create({ name: 'list', value: '$items', title: 'Test List' }); |
| 110 | + const ranker = RankerModel.create({ name: 'rank', toname: 'list', children: [ |
| 111 | + { id: 'B1', type: 'bucket', name: 'B1', title: 'Bucket 1' }, |
| 112 | + { id: 'B2', type: 'bucket', name: 'B2', title: 'Bucket 2', default: true }, |
| 113 | + ] }); |
| 114 | + const store = MockStore.create({ annotationStore: { selected: { names: { list, ranker } } } }); |
| 115 | + |
| 116 | + store.task.dataObj = { items }; |
| 117 | + list.updateValue(store); |
| 118 | + |
| 119 | + const columns = [ |
| 120 | + { id: 'B1', title: 'Bucket 1' }, |
| 121 | + { id: 'B2', title: 'Bucket 2' }, |
| 122 | + ]; |
| 123 | + const result: any = { |
| 124 | + from_name: ranker, |
| 125 | + to_name: list, |
| 126 | + type: 'ranker', |
| 127 | + value: { ranker: { B1: ['item2'], B2: ['item1', 'item3'] } }, |
| 128 | + }; |
| 129 | + |
| 130 | + it('List and Ranker should get values from the task', () => { |
| 131 | + expect(list._value).toEqual(items); |
| 132 | + expect(Object.keys(ranker.list.items)).toEqual(['item1', 'item2', 'item3']); |
| 133 | + }); |
| 134 | + |
| 135 | + it('Ranker should have proper columns and other getters', () => { |
| 136 | + expect(ranker.buckets.map((b: any) => b.name)).toEqual(['B1', 'B2']); |
| 137 | + expect(ranker.defaultBucket).toEqual('B2'); |
| 138 | + expect(ranker.rankOnly).toEqual(false); |
| 139 | + expect(ranker.columns).toEqual(columns); |
| 140 | + }); |
| 141 | + |
| 142 | + it('Ranker puts all items into default bucket if there is no result', () => { |
| 143 | + expect(ranker.dataSource).toEqual({ |
| 144 | + items: ranker.list.items, |
| 145 | + columns, |
| 146 | + itemIds: { B1: [], B2: ['item1', 'item2', 'item3'] }, |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('Ranker returns items according to result', () => { |
| 151 | + store.annotationStore.selected.results.push(result); |
| 152 | + |
| 153 | + expect(ranker.result).toBeTruthy(); |
| 154 | + expect(ranker.dataSource).toEqual({ |
| 155 | + items: ranker.list.items, |
| 156 | + columns, |
| 157 | + itemIds: { B1: ['item2'], B2: ['item1', 'item3'] }, |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments