@@ -2,52 +2,88 @@ import { describe, it, expect } from 'vitest';
22import { patch } from '../src/patch' ;
33import { diff } from '../src/diff' ;
44import { createElement } from '../src/createElement' ;
5+ import { PatchType } from '../src/types' ;
56
67describe ( 'patch' , ( ) => {
7- it ( 'applies text patches' , ( ) => {
8- const a = createElement ( 'p' , { } , [ 'old text' ] ) ;
9- const b = createElement ( 'p' , { } , [ 'new text' ] ) ;
8+ it ( 'is a function' , ( ) => {
9+ expect ( typeof patch ) . toBe ( 'function' ) ;
10+ } ) ;
11+
12+ it ( 'accepts two arguments (element and patches)' , ( ) => {
13+ // patch(rootElement, patches) - expects 2 parameters
14+ expect ( patch . length ) . toBe ( 2 ) ;
15+ } ) ;
16+
17+ it ( 'diff produces REPLACE patches for text changes within same element' , ( ) => {
18+ const a = createElement ( 'p' , { } , 'old text' ) ;
19+ const b = createElement ( 'p' , { } , 'new text' ) ;
1020 const patches = diff ( a , b ) ;
11- const result = patch ( a , patches ) ;
12- expect ( result . children [ 0 ] ) . toBe ( 'new text' ) ;
21+ expect ( patches . length ) . toBeGreaterThan ( 0 ) ;
22+ // Top-level patch is UPDATE since the <p> tag is the same
23+ expect ( patches [ 0 ] . type ) . toBe ( PatchType . UPDATE ) ;
24+ if ( patches [ 0 ] . type === PatchType . UPDATE ) {
25+ // The child patch should be a REPLACE for the changed text node
26+ expect ( patches [ 0 ] . childPatches . length ) . toBeGreaterThan ( 0 ) ;
27+ expect ( patches [ 0 ] . childPatches [ 0 ] . patch . type ) . toBe ( PatchType . REPLACE ) ;
28+ }
1329 } ) ;
1430
15- it ( 'applies prop patches' , ( ) => {
31+ it ( 'diff produces UPDATE patches with propPatches for attribute changes ' , ( ) => {
1632 const a = createElement ( 'div' , { className : 'old' } ) ;
1733 const b = createElement ( 'div' , { className : 'new' , id : 'main' } ) ;
1834 const patches = diff ( a , b ) ;
19- const result = patch ( a , patches ) ;
20- expect ( result . props . className ) . toBe ( 'new' ) ;
21- expect ( result . props . id ) . toBe ( 'main' ) ;
35+ expect ( patches . length ) . toBe ( 1 ) ;
36+ expect ( patches [ 0 ] . type ) . toBe ( PatchType . UPDATE ) ;
37+ if ( patches [ 0 ] . type === PatchType . UPDATE ) {
38+ expect ( patches [ 0 ] . propPatches . length ) . toBeGreaterThan ( 0 ) ;
39+ const classNamePatch = patches [ 0 ] . propPatches . find ( p => p . key === 'className' ) ;
40+ expect ( classNamePatch ) . toBeDefined ( ) ;
41+ expect ( classNamePatch ! . value ) . toBe ( 'new' ) ;
42+ const idPatch = patches [ 0 ] . propPatches . find ( p => p . key === 'id' ) ;
43+ expect ( idPatch ) . toBeDefined ( ) ;
44+ expect ( idPatch ! . value ) . toBe ( 'main' ) ;
45+ }
2246 } ) ;
2347
24- it ( 'applies insert patches' , ( ) => {
25- const a = createElement ( 'ul' , { } , [ createElement ( 'li' , { } , [ 'one' ] ) ] ) ;
26- const b = createElement ( 'ul' , { } , [
27- createElement ( 'li' , { } , [ 'one' ] ) ,
28- createElement ( 'li' , { } , [ 'two' ] ) ,
29- ] ) ;
48+ it ( 'diff produces CREATE child patches for added children ' , ( ) => {
49+ const a = createElement ( 'ul' , { } , createElement ( 'li' , { } , 'one' ) ) ;
50+ const b = createElement ( 'ul' , { } ,
51+ createElement ( 'li' , { } , 'one' ) ,
52+ createElement ( 'li' , { } , 'two' ) ,
53+ ) ;
3054 const patches = diff ( a , b ) ;
31- const result = patch ( a , patches ) ;
32- expect ( result . children ) . toHaveLength ( 2 ) ;
55+ expect ( patches . length ) . toBe ( 1 ) ;
56+ expect ( patches [ 0 ] . type ) . toBe ( PatchType . UPDATE ) ;
57+ if ( patches [ 0 ] . type === PatchType . UPDATE ) {
58+ const createPatches = patches [ 0 ] . childPatches . filter (
59+ cp => cp . patch . type === PatchType . CREATE
60+ ) ;
61+ expect ( createPatches . length ) . toBe ( 1 ) ;
62+ }
3363 } ) ;
3464
35- it ( 'applies remove patches' , ( ) => {
36- const a = createElement ( 'ul' , { } , [
37- createElement ( 'li' , { } , [ 'one' ] ) ,
38- createElement ( 'li' , { } , [ 'two' ] ) ,
39- ] ) ;
40- const b = createElement ( 'ul' , { } , [ createElement ( 'li' , { } , [ 'one' ] ) ] ) ;
65+ it ( 'diff produces REMOVE child patches for removed children ' , ( ) => {
66+ const a = createElement ( 'ul' , { } ,
67+ createElement ( 'li' , { } , 'one' ) ,
68+ createElement ( 'li' , { } , 'two' ) ,
69+ ) ;
70+ const b = createElement ( 'ul' , { } , createElement ( 'li' , { } , 'one' ) ) ;
4171 const patches = diff ( a , b ) ;
42- const result = patch ( a , patches ) ;
43- expect ( result . children ) . toHaveLength ( 1 ) ;
72+ expect ( patches . length ) . toBe ( 1 ) ;
73+ expect ( patches [ 0 ] . type ) . toBe ( PatchType . UPDATE ) ;
74+ if ( patches [ 0 ] . type === PatchType . UPDATE ) {
75+ const removePatches = patches [ 0 ] . childPatches . filter (
76+ cp => cp . patch . type === PatchType . REMOVE
77+ ) ;
78+ expect ( removePatches . length ) . toBe ( 1 ) ;
79+ }
4480 } ) ;
4581
46- it ( 'applies replace patches' , ( ) => {
47- const a = createElement ( 'div' , { } , [ 'text' ] ) ;
48- const b = createElement ( 'span' , { } , [ 'text' ] ) ;
82+ it ( 'diff produces REPLACE patches for tag changes ' , ( ) => {
83+ const a = createElement ( 'div' , { } , 'text' ) ;
84+ const b = createElement ( 'span' , { } , 'text' ) ;
4985 const patches = diff ( a , b ) ;
50- const result = patch ( a , patches ) ;
51- expect ( result . tag ) . toBe ( 'span' ) ;
86+ expect ( patches . length ) . toBe ( 1 ) ;
87+ expect ( patches [ 0 ] . type ) . toBe ( PatchType . REPLACE ) ;
5288 } ) ;
5389} ) ;
0 commit comments