import { render, screen } from '@testing-library/react';
import { hashLocationPlugin, servicesPlugin, UIRouterContext, UIRouterReact, UIViewContext } from '@uirouter/react';
import React from 'react';
import { AccountService } from '@spinnaker/core';
import { mockInstance } from '@spinnaker/mocks';
import { InstanceInformation } from './InstanceInformation';
describe('InstanceInformation', () => {
let router: UIRouterReact;
const testInstance = {
...mockInstance,
instanceType: 'm5.large',
capacityType: 'spot',
region: 'us-east-1',
serverGroup: 'test_sg',
};
const expectValue = (label: string, value: string) => {
const term = screen.getByText(label, { selector: 'dt' });
expect(term.nextElementSibling).toHaveTextContent(value);
};
const renderInformation = (component: React.ReactElement) =>
render(
{component}
,
);
beforeEach(() => {
vi.spyOn(AccountService, 'challengeDestructiveActions').mockResolvedValue(false);
router = new UIRouterReact();
router.plugin(servicesPlugin);
router.plugin(hashLocationPlugin);
['application', 'application.instance', 'application.serverGroup'].forEach((name) =>
router.stateRegistry.register({ name, url: `/${name.split('.').pop()}` }),
);
});
afterEach(() => router.dispose());
it('should render correct state when all attributes exist', () => {
renderInformation(
,
);
expectValue('Launched', '1970-01-14 22:37:37 PST');
expectValue('In', testInstance.availabilityZone);
expectValue('Type', testInstance.instanceType);
expectValue('Capacity Type', testInstance.capacityType);
expect(screen.getByRole('link', { name: testInstance.serverGroup })).toBeInTheDocument();
});
it('should render correct state when attributes are missing', () => {
renderInformation(
,
);
expectValue('Launched', 'Unknown');
expectValue('In', 'Unknown');
expectValue('Type', 'Unknown');
expect(screen.queryByText('Capacity Type', { selector: 'dt' })).not.toBeInTheDocument();
expect(screen.queryByText('Server Group', { selector: 'dt' })).not.toBeInTheDocument();
});
});