UNPKG

4.33 kBJavaScriptView Raw
1/* global ORE_OWNER_ACCOUNT_NAME:true */
2/* global ORE_TESTA_ACCOUNT_NAME:true */
3/* global ORE_NETWORK_URI:true */
4
5const { expectFetch, mock, mockInstrument, mockInstruments } = require('./helpers/fetch');
6const { constructOrejs } = require('./helpers/orejs');
7
8describe('instrument', () => {
9 let orejs;
10
11 beforeAll(() => {
12 orejs = constructOrejs();
13 });
14
15 describe('findInstruments', () => {
16 let instrumentMocks;
17 let active;
18 let additionalRighted;
19 let expired;
20 let uncategorized;
21
22 beforeEach(() => {
23 active = {
24 owner: ORE_TESTA_ACCOUNT_NAME,
25 };
26 expired = {
27 owner: ORE_TESTA_ACCOUNT_NAME,
28 end_time: Math.floor(Date.now() / 1000) - 10,
29 };
30 uncategorized = {
31 owner: ORE_TESTA_ACCOUNT_NAME,
32 instrument: {
33 instrument_class: 'company.uncategorized',
34 },
35 };
36 additionalRighted = {
37 owner: ORE_TESTA_ACCOUNT_NAME,
38 instrument: {
39 instrument_class: 'company.uncategorized',
40 rights: [{
41 right_name: 'company.nobody.licenseApi',
42 }],
43 },
44 };
45
46 instrumentMocks = mockInstruments([
47 active,
48 additionalRighted,
49 expired,
50 uncategorized,
51 ]);
52
53 fetch.resetMocks();
54 fetch.mockResponses(instrumentMocks);
55 orejs = constructOrejs({ fetch });
56 });
57
58 it('returns all active instruments for account', async () => {
59 const instruments = await orejs.findInstruments(ORE_TESTA_ACCOUNT_NAME);
60 expectFetch(`${ORE_NETWORK_URI}/v1/chain/get_table_rows`);
61 expect(instruments).toEqual([JSON.parse(instrumentMocks[0]).rows[0], JSON.parse(instrumentMocks[0]).rows[1], JSON.parse(instrumentMocks[0]).rows[3]]);
62 });
63
64 it('returns all instruments', async () => {
65 const instruments = await orejs.findInstruments(ORE_TESTA_ACCOUNT_NAME, false);
66 expectFetch(`${ORE_NETWORK_URI}/v1/chain/get_table_rows`);
67 expect(instruments).toEqual([JSON.parse(instrumentMocks[0]).rows[0], JSON.parse(instrumentMocks[0]).rows[1], JSON.parse(instrumentMocks[0]).rows[2], JSON.parse(instrumentMocks[0]).rows[3]]);
68 });
69
70 it('filters by category', async () => {
71 const instruments = await orejs.findInstruments(ORE_TESTA_ACCOUNT_NAME, true, 'company.uncategorized');
72 expectFetch(`${ORE_NETWORK_URI}/v1/chain/get_table_rows`);
73 expect(instruments).toEqual([JSON.parse(instrumentMocks[0]).rows[1], JSON.parse(instrumentMocks[0]).rows[3]]);
74 });
75
76 it('filters by right', async () => {
77 const instruments = await orejs.findInstruments(ORE_TESTA_ACCOUNT_NAME, true, 'company.uncategorized', 'company.nobody.licenseApi');
78 expectFetch(`${ORE_NETWORK_URI}/v1/chain/get_table_rows`);
79 expect(instruments).toEqual([JSON.parse(instrumentMocks[0]).rows[1]]);
80 });
81 });
82
83 describe('getRight', () => {
84 let instrument;
85 let rightName;
86 let rights;
87
88 beforeEach(() => {
89 rightName = 'company.somebody.licenseApi';
90 });
91
92 describe('when multiple rights exist', async () => {
93 beforeEach(() => {
94 rights = [{
95 right_name: 'company.left.licenseApi',
96 }, {
97 right_name: rightName,
98 }, {
99 right_name: 'company.right.licenseApi',
100 }];
101 instrument = mockInstrument({
102 owner: ORE_TESTA_ACCOUNT_NAME,
103 instrument: {
104 instrument_class: 'company.uncategorized',
105 rights,
106 },
107 });
108 });
109
110 it('returns the correct right', async () => {
111 const right = await orejs.getRight(instrument, rightName);
112 expect(right).toEqual(instrument.instrument.rights[1]);
113 });
114 });
115
116 describe('when the right does not exist', async () => {
117 beforeEach(() => {
118 rights = [{
119 right_name: 'company.left.licenseApi',
120 }, {
121 right_name: 'company.right.licenseApi',
122 }];
123 instrument = mockInstrument({
124 owner: ORE_TESTA_ACCOUNT_NAME,
125 instrument: {
126 instrument_class: 'company.uncategorized',
127 rights,
128 },
129 });
130 });
131
132 it('returns nothing', async () => {
133 const right = await orejs.getRight(instrument, rightName);
134 expect(right).toEqual(undefined);
135 });
136 });
137 });
138});