|
| 1 | +import TreeState from '../TreeState'; |
| 2 | +import {Nodes} from '../../../testData/sampleTree'; |
| 3 | + |
| 4 | +describe('TreeState', () => { |
| 5 | + describe('createFromTree', () => { |
| 6 | + test('should fail when falsy value is supplied', () => { |
| 7 | + expect(() => TreeState.createFromTree()).toThrowError('A falsy tree was supplied in tree creation'); |
| 8 | + expect(() => TreeState.createFromTree('')).toThrowError('A falsy tree was supplied in tree creation'); |
| 9 | + expect(() => TreeState.createFromTree(0)).toThrowError('A falsy tree was supplied in tree creation'); |
| 10 | + expect(() => TreeState.createFromTree(false)).toThrowError('A falsy tree was supplied in tree creation'); |
| 11 | + }); |
| 12 | + |
| 13 | + test('should fail when an invalid value is supplied', () => { |
| 14 | + expect(() => TreeState.createFromTree({})).toThrowError('An invalid tree was supplied in creation'); |
| 15 | + expect(() => TreeState.createFromTree('tree')).toThrowError('An invalid tree was supplied in creation'); |
| 16 | + expect(() => TreeState.createFromTree(1234)).toThrowError('An invalid tree was supplied in creation'); |
| 17 | + expect(() => TreeState.createFromTree(true)).toThrowError('An invalid tree was supplied in creation'); |
| 18 | + }); |
| 19 | + |
| 20 | + test('should create state when a valid tree is supplied', () => { |
| 21 | + const {tree, flattenedTree} = TreeState.createFromTree(Nodes); |
| 22 | + |
| 23 | + expect(tree).toEqual(Nodes); |
| 24 | + expect(flattenedTree).toMatchSnapshot(); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('getNodeAt', () => { |
| 29 | + test('should get a for an existing rowId', () => { |
| 30 | + expect(TreeState.getNodeAt(TreeState.createFromTree(Nodes), 0)).toBe(Nodes[0]); |
| 31 | + expect(TreeState.getNodeAt(TreeState.createFromTree(Nodes), 1)).toMatchSnapshot('2nd row'); |
| 32 | + expect(TreeState.getNodeAt(TreeState.createFromTree(Nodes), 2)).toMatchSnapshot('3rd row'); |
| 33 | + expect(TreeState.getNodeAt(TreeState.createFromTree(Nodes), 6)).toMatchSnapshot('7th row'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('should fail with a custom error when supplied rowId does not exist', () => { |
| 37 | + expect(() => TreeState.getNodeAt(TreeState.createFromTree(Nodes), 25)).toThrowErrorMatchingSnapshot(); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should fail for when invalid state is supplied', () => { |
| 41 | + expect(() => TreeState.getNodeAt('state', 0)).toThrowError('Expected a State instance but got string'); |
| 42 | + expect(() => TreeState.getNodeAt(1225, 0)).toThrowError('Expected a State instance but got number'); |
| 43 | + expect(() => TreeState.getNodeAt([], 0)).toThrowError('Expected a State instance but got object'); |
| 44 | + expect(() => TreeState.getNodeAt({}, 0)).toThrowError('Expected a State instance but got object'); |
| 45 | + expect(() => TreeState.getNodeAt(true, 0)).toThrowError('Expected a State instance but got boolean'); |
| 46 | + expect(() => TreeState.getNodeAt(() => {}, 0)).toThrowError('Expected a State instance but got function'); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('getTree', () => { |
| 51 | + test('should get a tree', () => { |
| 52 | + expect(TreeState.getTree(TreeState.createFromTree(Nodes))).toEqual(Nodes); |
| 53 | + }); |
| 54 | + |
| 55 | + test('should fail for when invalid state is supplied', () => { |
| 56 | + expect(() => TreeState.getTree('state')).toThrowError('Expected a State instance but got string'); |
| 57 | + expect(() => TreeState.getTree(1225)).toThrowError('Expected a State instance but got number'); |
| 58 | + expect(() => TreeState.getTree([])).toThrowError('Expected a State instance but got object'); |
| 59 | + expect(() => TreeState.getTree({})).toThrowError('Expected a State instance but got object'); |
| 60 | + expect(() => TreeState.getTree(true)).toThrowError('Expected a State instance but got boolean'); |
| 61 | + expect(() => TreeState.getTree(() => {})).toThrowError('Expected a State instance but got function'); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments