1+ import type { AttributeGroupDraft } from "@commercetools/platform-sdk" ;
2+ import { describe , expect , test } from "vitest" ;
3+ import type { Config } from "~src/config" ;
4+ import { InMemoryStorage } from "~src/storage" ;
5+ import { AttributeGroupRepository } from "./attribute-group" ;
6+
7+ describe ( "AttributeGroup Repository" , ( ) => {
8+ const storage = new InMemoryStorage ( ) ;
9+ const config : Config = { storage, strict : false } ;
10+ const repository = new AttributeGroupRepository ( config ) ;
11+
12+ test ( "create attribute group" , ( ) => {
13+ const draft : AttributeGroupDraft = {
14+ name : { "en-US" : "Size Attributes" , "de-DE" : "Größenattribute" } ,
15+ description : { "en-US" : "Attributes related to product size" } ,
16+ key : "size-attributes" ,
17+ attributes : [
18+ {
19+ key : "size" ,
20+ } ,
21+ {
22+ key : "weight" ,
23+ } ,
24+ ] ,
25+ } ;
26+
27+ const ctx = { projectKey : "dummy" } ;
28+ const result = repository . create ( ctx , draft ) ;
29+
30+ expect ( result . id ) . toBeDefined ( ) ;
31+ expect ( result . version ) . toBe ( 1 ) ;
32+ expect ( result . name ) . toEqual ( draft . name ) ;
33+ expect ( result . description ) . toEqual ( draft . description ) ;
34+ expect ( result . key ) . toBe ( draft . key ) ;
35+ expect ( result . attributes ) . toEqual ( draft . attributes ) ;
36+
37+ // Test that the attribute group is stored
38+ const items = repository . query ( ctx ) ;
39+ expect ( items . count ) . toBe ( 1 ) ;
40+ expect ( items . results [ 0 ] . id ) . toBe ( result . id ) ;
41+ } ) ;
42+
43+ test ( "create attribute group with minimal data" , ( ) => {
44+ const draft : AttributeGroupDraft = {
45+ name : { "en-US" : "Minimal Attributes" } ,
46+ } ;
47+
48+ const ctx = { projectKey : "dummy" } ;
49+ const result = repository . create ( ctx , draft ) ;
50+
51+ expect ( result . id ) . toBeDefined ( ) ;
52+ expect ( result . name ) . toEqual ( draft . name ) ;
53+ expect ( result . description ) . toBeUndefined ( ) ;
54+ expect ( result . key ) . toBeUndefined ( ) ;
55+ expect ( result . attributes ) . toBeUndefined ( ) ;
56+ } ) ;
57+
58+ test ( "update attribute group - changeName" , ( ) => {
59+ const draft : AttributeGroupDraft = {
60+ name : { "en-US" : "Original Name" } ,
61+ key : "test-attributes" ,
62+ } ;
63+
64+ const ctx = { projectKey : "dummy" } ;
65+ const attributeGroup = repository . create ( ctx , draft ) ;
66+
67+ const result = repository . processUpdateActions ( ctx , attributeGroup , attributeGroup . version , [
68+ {
69+ action : "changeName" ,
70+ name : { "en-US" : "Updated Name" , "de-DE" : "Aktualisierter Name" } ,
71+ } ,
72+ ] ) ;
73+
74+ expect ( result . name ) . toEqual ( { "en-US" : "Updated Name" , "de-DE" : "Aktualisierter Name" } ) ;
75+ expect ( result . version ) . toBe ( attributeGroup . version + 1 ) ;
76+ } ) ;
77+
78+ test ( "update attribute group - setDescription" , ( ) => {
79+ const draft : AttributeGroupDraft = {
80+ name : { "en-US" : "Test Attributes" } ,
81+ key : "test-attributes-2" ,
82+ } ;
83+
84+ const ctx = { projectKey : "dummy" } ;
85+ const attributeGroup = repository . create ( ctx , draft ) ;
86+
87+ const result = repository . processUpdateActions ( ctx , attributeGroup , attributeGroup . version , [
88+ {
89+ action : "setDescription" ,
90+ description : { "en-US" : "New description" , "de-DE" : "Neue Beschreibung" } ,
91+ } ,
92+ ] ) ;
93+
94+ expect ( result . description ) . toEqual ( { "en-US" : "New description" , "de-DE" : "Neue Beschreibung" } ) ;
95+ expect ( result . version ) . toBe ( attributeGroup . version + 1 ) ;
96+ } ) ;
97+
98+ test ( "update attribute group - setKey" , ( ) => {
99+ const draft : AttributeGroupDraft = {
100+ name : { "en-US" : "Key Test Attributes" } ,
101+ key : "original-key" ,
102+ } ;
103+
104+ const ctx = { projectKey : "dummy" } ;
105+ const attributeGroup = repository . create ( ctx , draft ) ;
106+
107+ const result = repository . processUpdateActions ( ctx , attributeGroup , attributeGroup . version , [
108+ {
109+ action : "setKey" ,
110+ key : "updated-key" ,
111+ } ,
112+ ] ) ;
113+
114+ expect ( result . key ) . toBe ( "updated-key" ) ;
115+ expect ( result . version ) . toBe ( attributeGroup . version + 1 ) ;
116+ } ) ;
117+
118+ test ( "update attribute group - setAttributes" , ( ) => {
119+ const draft : AttributeGroupDraft = {
120+ name : { "en-US" : "Attributes Test" } ,
121+ key : "attributes-test" ,
122+ attributes : [
123+ {
124+ key : "original-attribute" ,
125+ } ,
126+ ] ,
127+ } ;
128+
129+ const ctx = { projectKey : "dummy" } ;
130+ const attributeGroup = repository . create ( ctx , draft ) ;
131+
132+ const result = repository . processUpdateActions ( ctx , attributeGroup , attributeGroup . version , [
133+ {
134+ action : "setAttributes" ,
135+ attributes : [
136+ {
137+ key : "new-attribute-1" ,
138+ } ,
139+ {
140+ key : "new-attribute-2" ,
141+ } ,
142+ ] ,
143+ } ,
144+ ] ) ;
145+
146+ expect ( result . attributes ) . toEqual ( [
147+ { key : "new-attribute-1" } ,
148+ { key : "new-attribute-2" } ,
149+ ] ) ;
150+ expect ( result . version ) . toBe ( attributeGroup . version + 1 ) ;
151+ } ) ;
152+
153+ test ( "get and delete attribute group" , ( ) => {
154+ const draft : AttributeGroupDraft = {
155+ name : { "en-US" : "Delete Test Attributes" } ,
156+ key : "delete-test" ,
157+ } ;
158+
159+ const ctx = { projectKey : "dummy" } ;
160+ const attributeGroup = repository . create ( ctx , draft ) ;
161+
162+ // Test get
163+ const retrieved = repository . get ( ctx , attributeGroup . id ) ;
164+ expect ( retrieved ) . toBeDefined ( ) ;
165+ expect ( retrieved ?. id ) . toBe ( attributeGroup . id ) ;
166+
167+ // Test getByKey
168+ const retrievedByKey = repository . getByKey ( ctx , attributeGroup . key ! ) ;
169+ expect ( retrievedByKey ) . toBeDefined ( ) ;
170+ expect ( retrievedByKey ?. key ) . toBe ( attributeGroup . key ) ;
171+
172+ // Test delete
173+ const deleted = repository . delete ( ctx , attributeGroup . id ) ;
174+ expect ( deleted ) . toBeDefined ( ) ;
175+ expect ( deleted ?. id ) . toBe ( attributeGroup . id ) ;
176+
177+ // Verify it's deleted
178+ const notFound = repository . get ( ctx , attributeGroup . id ) ;
179+ expect ( notFound ) . toBeNull ( ) ;
180+ } ) ;
181+ } ) ;
0 commit comments