import {
    getAttributeValue,
    getItemAttributeByIdentifier,
    getItemAttributeValueByIdentifier,
} from '../AttributeSelectors';

describe('Reducers/Configurator/Selectors', () => {
    const item = {
        attributes: [
            {
                attribute_id: null,
                attributedatatype: 'string',
                identifier: 'attribute_1',
                texts: null,
                title: 'Attribute 1',
                translatedTitle: null,
                values: [
                    {
                        value: '1',
                        translated_value: '1',
                    },
                    {
                        value: '2',
                        translated_value: '2',
                    },
                ],
            },
            {
                attribute_id: null,
                attributedatatype: 'string',
                identifier: 'attribute_2',
                texts: null,
                title: 'Attribute 2',
                translatedTitle: null,
                values: [
                    {
                        value: '3',
                        translated_value: '1',
                    },
                    {
                        value: '4',
                        translated_value: '2',
                    },
                ],
            },
        ],
    };

    test('getItemAttributeByIdentifier returns the matching attribute', () => {
        const attribute = getItemAttributeByIdentifier(item, 'attribute_2');
        expect(attribute.identifier).toBe('attribute_2');
    });

    test('getAttributeValue returns the first from the attribute values', () => {
        expect(getAttributeValue(item.attributes[1])).toBe('3');
    });

    test('getAttributeValue returns the default value if the attribute does not contain any value', () => {
        expect(getAttributeValue({}, 'dflt')).toBe('dflt');
    });

    test('getItemAttributeValueByIdentifier returns the first value for the given attribute identifier', () => {
        expect(getItemAttributeValueByIdentifier(item, 'attribute_2')).toBe('3');
    });

    test('getItemAttributeValueByIdentifier returns the default value if the attribute does not exist', () => {
        expect(getItemAttributeValueByIdentifier(item, 'attribute_3', 'dflt')).toBe('dflt');
    });
});
