Skip to content

Commit 6ba097f

Browse files
committed
Address PR review feedback: restructure tests and simplify asap usage
1 parent c1f81bf commit 6ba097f

File tree

2 files changed

+72
-65
lines changed

2 files changed

+72
-65
lines changed

src/sinks/class-sink.test.ts

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,81 +30,92 @@ describe('Class Sink', () => {
3030

3131
describe('Given a class object', () => {
3232

33-
it('sets classes for truthy attributes on sink', () => {
34-
const el = MockElement();
35-
const sink = ClassObjectSink(<HTMLElement>el);
33+
describe('when a property of the object is a present value', () => {
34+
35+
it('sets classes for truthy attributes on sink', () => {
36+
const el = MockElement();
37+
const sink = ClassObjectSink(<HTMLElement>el);
38+
39+
sink({
40+
class1: true,
41+
class2: 1,
42+
class3: 'yes!',
43+
});
44+
expect(el.className).toContain('class1');
45+
expect(el.className).toContain('class2');
46+
expect(el.className).toContain('class3');
47+
});
3648

37-
sink({
38-
class1: true,
39-
class2: 1,
40-
class3: 'yes!',
49+
it('clears classes for falsy attributes on sink', () => {
50+
const el = MockElement({ className: 'class1 class2 class3' });
51+
const sink = ClassObjectSink(<HTMLElement>el);
52+
expect(el.className).toContain('class1');
53+
expect(el.className).toContain('class2');
54+
expect(el.className).toContain('class3');
55+
56+
sink({
57+
class1: false,
58+
class2: 0,
59+
class3: '',
60+
});
61+
expect(el.className).not.toContain('class1');
62+
expect(el.className).not.toContain('class2');
63+
expect(el.className).not.toContain('class3');
4164
});
42-
expect(el.className).toContain('class1');
43-
expect(el.className).toContain('class2');
44-
expect(el.className).toContain('class3');
45-
});
4665

47-
it('clears classes for falsy attributes on sink', () => {
48-
const el = MockElement({ className: 'class1 class2 class3' });
49-
const sink = ClassObjectSink(<HTMLElement>el);
50-
expect(el.className).toContain('class1');
51-
expect(el.className).toContain('class2');
52-
expect(el.className).toContain('class3');
66+
it('should not add class when value is false', () => {
67+
const el = MockElement();
68+
const sink = ClassObjectSink(<HTMLElement>el);
5369

54-
sink({
55-
class1: false,
56-
class2: 0,
57-
class3: '',
70+
sink({
71+
dotted: false,
72+
});
73+
74+
expect(el.className).not.toContain('dotted');
75+
expect(el.className).toEqual('');
5876
});
59-
expect(el.className).not.toContain('class1');
60-
expect(el.className).not.toContain('class2');
61-
expect(el.className).not.toContain('class3');
77+
6278
});
6379

64-
it('should not add class when value is false (promise resolved to false)', () => {
65-
const el = MockElement();
66-
const sink = ClassObjectSink(<HTMLElement>el);
80+
describe('when a property of the object is a future value', () => {
6781

68-
// Simulating a promise that resolved to false
69-
sink({
70-
dotted: false,
71-
});
82+
describe('when it resolves/emits false', () => {
7283

73-
expect(el.className).not.toContain('dotted');
74-
expect(el.className).toEqual('');
75-
});
84+
it('should not add a class corresponding to the property name', async () => {
85+
const el = MockElement();
86+
const sink = ClassObjectSink(<HTMLElement>el);
7687

77-
it('should not add class when promise resolves to false', async () => {
78-
const el = MockElement();
79-
const sink = ClassObjectSink(<HTMLElement>el);
88+
const dottedPromise = Promise.resolve(false);
89+
sink({
90+
dotted: dottedPromise,
91+
});
92+
93+
await dottedPromise;
94+
95+
expect(el.className).not.toContain('dotted');
96+
expect(el.className).toEqual('');
97+
});
8098

81-
// Pass a promise that resolves to false
82-
const dottedPromise = Promise.resolve(false);
83-
sink({
84-
dotted: dottedPromise,
8599
});
86100

87-
// Wait for the promise to resolve
88-
await dottedPromise;
101+
describe('when it resolves/emits true', () => {
89102

90-
expect(el.className).not.toContain('dotted');
91-
expect(el.className).toEqual('');
92-
});
103+
it('should add a class corresponding to the property name', async () => {
104+
const el = MockElement();
105+
const sink = ClassObjectSink(<HTMLElement>el);
93106

94-
it('should add class when promise resolves to true', async () => {
95-
const el = MockElement();
96-
const sink = ClassObjectSink(<HTMLElement>el);
107+
const activePromise = Promise.resolve(true);
108+
sink({
109+
active: activePromise,
110+
});
97111

98-
// Pass a promise that resolves to true
99-
const activePromise = Promise.resolve(true);
100-
sink({
101-
active: activePromise,
102-
});
112+
await activePromise;
103113

104-
// Wait for the promise to resolve
105-
await activePromise;
114+
expect(el.className).toContain('active');
115+
});
116+
117+
});
106118

107-
expect(el.className).toContain('active');
108119
});
109120

110121
});

src/sinks/class-sink.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Sink, ExplicitSink } from "../types/sink";
44

55
import { SINK_TAG } from "../constants";
66
import { asap } from "../lib/drain";
7-
import { isFuture } from "../types/futures";
87

98
export const TOGGLE_CLASS_SINK_TAG = 'ToggleClass';
109
export const CLASS_SINK_TAG = 'class'; // Keeping it same as 'class" attribute for now. Don't change yet...
@@ -37,13 +36,10 @@ export const ClassObjectSink: Sink<Element> = (node: Element) => {
3736
: (<(ClassName | ClassRecord)[]>[]).concat(name).forEach(obj => Object.entries(obj)
3837
// TODO: support 3-state with toggle
3938
.forEach(([k, v]) => {
40-
// If v is a future (Promise or Observable), defer the decision
41-
// until it resolves, otherwise check immediately
42-
if (isFuture(v)) {
43-
asap((resolvedValue: any) => resolvedValue ? add(k) : remove(k), v);
44-
} else {
45-
asap(v ? add : remove, k);
46-
}
39+
// Use asap to handle both present and future values
40+
// For futures (Promise/Observable), it will wait for resolution
41+
// For present values, it will execute immediately
42+
asap((resolvedValue: any) => resolvedValue ? add(k) : remove(k), v);
4743
})
4844
)
4945
};

0 commit comments

Comments
 (0)