-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFieldChangeItem.test.tsx
More file actions
119 lines (109 loc) · 3.83 KB
/
FieldChangeItem.test.tsx
File metadata and controls
119 lines (109 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { FieldChangeItem } from '../FieldChangeItem';
import type { FieldChangeEntry } from '@object-ui/types';
describe('FieldChangeItem', () => {
it('should render field label with old and new display values', () => {
const change: FieldChangeEntry = {
field: 'status',
fieldLabel: 'Status',
oldDisplayValue: 'Open',
newDisplayValue: 'Closed',
};
render(<FieldChangeItem change={change} />);
expect(screen.getByText('Status')).toBeInTheDocument();
expect(screen.getByText('Open')).toBeInTheDocument();
expect(screen.getByText('Closed')).toBeInTheDocument();
});
it('should derive field label from field name when fieldLabel is not set', () => {
const change: FieldChangeEntry = {
field: 'first_name',
oldValue: 'John',
newValue: 'Jane',
};
render(<FieldChangeItem change={change} />);
expect(screen.getByText('First name')).toBeInTheDocument();
});
it('should use raw values when display values are not set', () => {
const change: FieldChangeEntry = {
field: 'priority',
fieldLabel: 'Priority',
oldValue: 'low',
newValue: 'high',
};
render(<FieldChangeItem change={change} />);
expect(screen.getByText('low')).toBeInTheDocument();
expect(screen.getByText('high')).toBeInTheDocument();
});
it('should show (empty) when value is null/undefined', () => {
const change: FieldChangeEntry = {
field: 'notes',
fieldLabel: 'Notes',
newValue: 'Some text',
};
render(<FieldChangeItem change={change} />);
expect(screen.getByText('(empty)')).toBeInTheDocument();
expect(screen.getByText('Some text')).toBeInTheDocument();
});
it('should apply custom className', () => {
const change: FieldChangeEntry = {
field: 'name',
fieldLabel: 'Name',
oldValue: 'A',
newValue: 'B',
};
const { container } = render(<FieldChangeItem change={change} className="custom-class" />);
expect(container.firstChild).toHaveClass('custom-class');
});
it('should render arrow icon between old and new values', () => {
const change: FieldChangeEntry = {
field: 'status',
fieldLabel: 'Status',
oldValue: 'Open',
newValue: 'Closed',
};
const { container } = render(<FieldChangeItem change={change} />);
// ArrowRight renders as an SVG with lucide classes
const svg = container.querySelector('svg');
expect(svg).toBeInTheDocument();
});
it('should render old value with line-through style', () => {
const change: FieldChangeEntry = {
field: 'status',
fieldLabel: 'Status',
oldDisplayValue: 'Open',
newDisplayValue: 'Closed',
};
render(<FieldChangeItem change={change} />);
const oldEl = screen.getByText('Open');
expect(oldEl).toHaveClass('line-through');
});
it('should use fieldLabel priority over auto-generated label', () => {
const change: FieldChangeEntry = {
field: 'first_name',
fieldLabel: 'Custom Label',
oldValue: 'A',
newValue: 'B',
};
render(<FieldChangeItem change={change} />);
expect(screen.getByText('Custom Label')).toBeInTheDocument();
expect(screen.queryByText('First name')).not.toBeInTheDocument();
});
it('should show (empty) for both null old and new values', () => {
const change: FieldChangeEntry = {
field: 'notes',
fieldLabel: 'Notes',
};
render(<FieldChangeItem change={change} />);
const emptyTexts = screen.getAllByText('(empty)');
expect(emptyTexts).toHaveLength(2);
});
});