HASensorModal.test.tsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * The Home Assistant sensor modal (#1148, #448).
  3. *
  4. * Focused on the unconfigured-Home-Assistant path, which is the state a first
  5. * time user is actually in: the entity picker can only ever come back empty
  6. * there, and an empty list reads as "I have no sensors" rather than as "you
  7. * have not connected Home Assistant yet".
  8. */
  9. import { screen, waitFor } from '@testing-library/react';
  10. import { beforeEach, describe, expect, it, vi } from 'vitest';
  11. import { HASensorModal } from '../../components/HASensorModal';
  12. import { api } from '../../api/client';
  13. import { render } from '../utils';
  14. vi.mock('../../api/client', async () => {
  15. const actual = await vi.importActual<typeof import('../../api/client')>('../../api/client');
  16. return {
  17. ...actual,
  18. api: { ...actual.api, getSettings: vi.fn(), getBindableHAEntities: vi.fn() },
  19. };
  20. });
  21. const getSettings = vi.mocked(api.getSettings);
  22. const getEntities = vi.mocked(api.getBindableHAEntities);
  23. const PRINTERS = [{ id: 4, name: 'X1C-1' }] as never;
  24. function settings(overrides = {}) {
  25. return {
  26. ha_enabled: true,
  27. ha_url: 'http://homeassistant.local:8123',
  28. ha_token: 'token',
  29. ...overrides,
  30. } as never;
  31. }
  32. describe('HASensorModal', () => {
  33. beforeEach(() => {
  34. getSettings.mockReset();
  35. getEntities.mockReset();
  36. getEntities.mockResolvedValue([]);
  37. });
  38. it('warns when Home Assistant is not configured at all', async () => {
  39. getSettings.mockResolvedValue(settings({ ha_enabled: false, ha_url: '', ha_token: '' }));
  40. render(<HASensorModal printers={PRINTERS} onClose={() => {}} />);
  41. expect(
  42. await screen.findByText(/Home Assistant is not configured/)
  43. ).toBeInTheDocument();
  44. expect(screen.getByText('Settings → Network → Home Assistant')).toBeInTheDocument();
  45. });
  46. it('warns when the integration is configured but switched off', async () => {
  47. getSettings.mockResolvedValue(settings({ ha_enabled: false }));
  48. render(<HASensorModal printers={PRINTERS} onClose={() => {}} />);
  49. expect(await screen.findByText(/Home Assistant is not configured/)).toBeInTheDocument();
  50. });
  51. it('does not ask Home Assistant for entities it cannot reach', async () => {
  52. getSettings.mockResolvedValue(settings({ ha_token: '' }));
  53. render(<HASensorModal printers={PRINTERS} onClose={() => {}} />);
  54. await screen.findByText(/Home Assistant is not configured/);
  55. expect(getEntities).not.toHaveBeenCalled();
  56. });
  57. it('blocks saving a new sensor while unconfigured', async () => {
  58. getSettings.mockResolvedValue(settings({ ha_enabled: false }));
  59. render(<HASensorModal printers={PRINTERS} onClose={() => {}} />);
  60. await screen.findByText(/Home Assistant is not configured/);
  61. expect(screen.getByRole('button', { name: /save/i })).toBeDisabled();
  62. });
  63. it('shows no warning once Home Assistant is configured', async () => {
  64. getSettings.mockResolvedValue(settings());
  65. render(<HASensorModal printers={PRINTERS} onClose={() => {}} />);
  66. await waitFor(() => expect(getEntities).toHaveBeenCalled());
  67. expect(screen.queryByText(/Home Assistant is not configured/)).not.toBeInTheDocument();
  68. });
  69. });