import { createRowKey } from '../rows';

describe('createRowKey', () => {
    test('returns the value of the given property', () => {
        const value = 'hey';
        const result = createRowKey({ value }, 'value', []);

        expect(result).toBe(value);
    });

    test('returns string value of the given property', () => {
        const value = 12345;
        const result = createRowKey({ value }, 'value', []);

        expect(result).toBe(value.toString());
    });

    describe('when no key given', () => {
        test('defaults to "key"', () => {
            const key = 'hey';
            const result = createRowKey({ key }, undefined, []);

            expect(result).toBe(key);
        });

        test('fallbacks to combining row values', () => {
            const result = createRowKey({ col1: '1', col2: '2' }, undefined, ['col1', 'col2']);

            expect(result).toBe('1_2');
        });
    });

    describe('when row key is a function', () => {
        test('invokes function', () => {
            const key = 'hey';
            const other = 'ho';
            const result = createRowKey({ key, other }, (row) => row.other, []);

            expect(result).toBe(other);
        });
    });
});
