Skip to content

Commit 2ce7279

Browse files
chore(test-utils): fix mock upload handler to correctly mock file overwrites (#6561)
* chore(test-utils): fix mock upload overwrite behavior * chore: refactor approach * chore: fix missing assignment
1 parent a004c2f commit 2ce7279

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

packages/test-utils/src/storage-browser/mock-handlers.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ export class MockHandlers {
124124

125125
const prefix = key.slice(0, -name.length);
126126

127-
if (hasWebkitRelativePath && !this.#locationItems[prefix]) {
128-
// register folder prefix
127+
// register folder prefix if missing
128+
if (!this.#locationItems[prefix]) {
129129
this.#locationItems[prefix] = [];
130+
}
130131

132+
if (hasWebkitRelativePath) {
131133
// create new folder within parent prefix
132134
const parentPrefix = key.slice(0, -webkitRelativePath.length);
133135
this.#locationItems[parentPrefix].push({
@@ -137,17 +139,21 @@ export class MockHandlers {
137139
});
138140
}
139141

140-
if (
141-
preventOverwrite &&
142-
this.#locationItems[prefix].some((item) => item.key === key)
143-
) {
144-
return {
145-
...UNDEFINED_CALLBACKS,
146-
result: Promise.resolve({
147-
error: new Error('cannot overwrite existing file'),
148-
status: 'OVERWRITE_PREVENTED',
149-
}),
150-
};
142+
if (this.#locationItems[prefix].some((item) => item.key === key)) {
143+
if (preventOverwrite) {
144+
return {
145+
...UNDEFINED_CALLBACKS,
146+
result: Promise.resolve({
147+
error: new Error('cannot overwrite existing file'),
148+
status: 'OVERWRITE_PREVENTED',
149+
}),
150+
};
151+
} else {
152+
// remove prev item as new version will be added, 'overwriting' prev item
153+
this.#locationItems[prefix] = this.#locationItems[prefix].filter(
154+
(item) => item.key !== key
155+
);
156+
}
151157
}
152158

153159
this.#locationItems[prefix].push({

0 commit comments

Comments
 (0)