InventorySpoolInfoCard.test.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { describe, it, expect, vi, beforeEach } from 'vitest';
  2. import { screen, fireEvent } from '@testing-library/react';
  3. import { render } from '../utils';
  4. import { InventorySpoolInfoCard } from '../../components/spoolbuddy/InventorySpoolInfoCard';
  5. import type { InventorySpool } from '../../api/client';
  6. vi.mock('../../api/client', () => ({
  7. api: {
  8. getSettings: vi.fn().mockResolvedValue({}),
  9. getAuthStatus: vi.fn().mockResolvedValue({ auth_enabled: false }),
  10. getSpoolKProfiles: vi.fn().mockResolvedValue([]),
  11. },
  12. spoolbuddyApi: {
  13. updateSpoolWeight: vi.fn().mockResolvedValue({ status: 'ok', weight_used: 300 }),
  14. },
  15. }));
  16. const mockSpool: InventorySpool = {
  17. id: 42,
  18. material: 'PLA',
  19. subtype: 'Matte',
  20. color_name: 'Jade White',
  21. rgba: 'E8F5E9FF',
  22. extra_colors: null,
  23. effect_type: null,
  24. brand: 'Bambu',
  25. label_weight: 1000,
  26. core_weight: 250,
  27. core_weight_catalog_id: null,
  28. weight_used: 200,
  29. slicer_filament: null,
  30. slicer_filament_name: null,
  31. nozzle_temp_min: null,
  32. nozzle_temp_max: null,
  33. note: null,
  34. added_full: null,
  35. last_used: null,
  36. encode_time: null,
  37. tag_uid: 'AABBCCDD11223344',
  38. tray_uuid: null,
  39. data_origin: 'local',
  40. tag_type: null,
  41. archived_at: null,
  42. created_at: '2024-01-01T00:00:00Z',
  43. updated_at: '2024-01-01T00:00:00Z',
  44. cost_per_kg: null,
  45. last_scale_weight: null,
  46. last_weighed_at: null,
  47. category: null,
  48. low_stock_threshold_pct: null,
  49. k_profiles: [],
  50. };
  51. describe('InventorySpoolInfoCard', () => {
  52. beforeEach(() => {
  53. vi.clearAllMocks();
  54. });
  55. it('disables "Assign to AMS" button when isAssigned=true', () => {
  56. render(
  57. <InventorySpoolInfoCard
  58. spool={mockSpool}
  59. liveScaleWeight={null}
  60. onAssignToAms={vi.fn()}
  61. isAssigned
  62. />
  63. );
  64. expect(screen.getByText('Assign to AMS')).toBeDisabled();
  65. });
  66. it('enables "Assign to AMS" button when isAssigned=false', () => {
  67. render(
  68. <InventorySpoolInfoCard
  69. spool={mockSpool}
  70. liveScaleWeight={null}
  71. onAssignToAms={vi.fn()}
  72. isAssigned={false}
  73. />
  74. );
  75. expect(screen.getByText('Assign to AMS')).not.toBeDisabled();
  76. });
  77. it('shows Unassign button when isAssigned=true and onUnassignFromAms is provided', () => {
  78. render(
  79. <InventorySpoolInfoCard
  80. spool={mockSpool}
  81. liveScaleWeight={null}
  82. onAssignToAms={vi.fn()}
  83. isAssigned
  84. onUnassignFromAms={vi.fn()}
  85. />
  86. );
  87. expect(screen.getByText(/unassign/i)).toBeInTheDocument();
  88. });
  89. it('does not show Unassign button when onUnassignFromAms is not provided', () => {
  90. render(
  91. <InventorySpoolInfoCard
  92. spool={mockSpool}
  93. liveScaleWeight={null}
  94. onAssignToAms={vi.fn()}
  95. isAssigned
  96. />
  97. );
  98. expect(screen.queryByText(/unassign/i)).not.toBeInTheDocument();
  99. });
  100. it('calls onUnassignFromAms when Unassign button is clicked', () => {
  101. const onUnassign = vi.fn();
  102. render(
  103. <InventorySpoolInfoCard
  104. spool={mockSpool}
  105. liveScaleWeight={null}
  106. onAssignToAms={vi.fn()}
  107. isAssigned
  108. onUnassignFromAms={onUnassign}
  109. />
  110. );
  111. fireEvent.click(screen.getByText(/unassign/i));
  112. expect(onUnassign).toHaveBeenCalledTimes(1);
  113. });
  114. });