/**
 * Copyright (C) 2026 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// src/AnyHeader.tsx(AnyHeader 任意自定义 Header 包装组件)白盒单元测试

jest.mock(
    'react-native/Libraries/Utilities/codegenNativeComponent',
    () => jest.fn((name) => name),
    { virtual: true },
);

function loadAnyHeader() {
    jest.resetModules();
    const codegenNativeComponent = require('react-native/Libraries/Utilities/codegenNativeComponent');
    const AnyHeader = require('../../../src/AnyHeader').default;
    return { AnyHeader, codegenNativeComponent };
}

describe('src/AnyHeader.tsx(AnyHeader 自定义 Header 包装组件)', () => {
    it('依赖的 RNCAnyHeader 通过 codegenNativeComponent 注册', () => {
        const { codegenNativeComponent } = loadAnyHeader();
        expect(codegenNativeComponent).toHaveBeenCalledWith('RNCAnyHeader');
    });

    it('渲染 RNCAnyHeader,style 强制为 { height: 0 }', () => {
        const { AnyHeader } = loadAnyHeader();
        // 直接实例化 class 组件做白盒浅渲染
        const inst = new AnyHeader({});
        const tree = inst.render();
        expect(tree.type).toBe('RNCAnyHeader');
        expect(tree.props.style).toEqual({ height: 0 });
    });

    it('children 渲染在 RNCAnyHeader 内', () => {
        const { AnyHeader } = loadAnyHeader();
        const children = { type: 'CustomSpinner', props: {}, key: null };
        const inst = new AnyHeader({ children });
        expect(inst.render().props.children).toBe(children);
    });

    it('其余 props(如 primaryColor)原样透传', () => {
        const { AnyHeader } = loadAnyHeader();
        const inst = new AnyHeader({ primaryColor: '#ff0000' });
        const { props } = inst.render();
        expect(props.primaryColor).toBe('#ff0000');
        expect(props.style).toEqual({ height: 0 });
    });

    it('覆盖传入的 style 时 height 仍为 0', () => {
        const { AnyHeader } = loadAnyHeader();
        // 源码中 style 固定写死为 { height: 0 },不与传入 style 合并
        const inst = new AnyHeader({ style: [{ height: 100 }] });
        expect(inst.render().props.style).toEqual({ height: 0 });
    });
});