-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Dulajdeshan/main
Release
- Loading branch information
Showing
12 changed files
with
1,586 additions
and
1,312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,135 @@ | ||
import { v4, validate } from "uuid"; | ||
import bootstrap from "../server/bootstrap"; | ||
import { handleYupError } from '@strapi/utils'; // Mock this function as needed | ||
|
||
const strapi = { | ||
db: { | ||
lifecycles: { | ||
subscribe: jest.fn(), | ||
}, | ||
}, | ||
contentTypes: { | ||
'api::firstTestModel': { | ||
attributes: { | ||
actualUUID: { | ||
customField: "plugin::strapi-advanced-uuid.uuid", | ||
jest.mock('@strapi/utils', () => ({ | ||
handleYupError: jest.fn(), | ||
})); | ||
|
||
describe('Strapi Lifecycle Methods for Different Models', () => { | ||
let strapiMock; | ||
|
||
beforeEach(() => { | ||
// Clear any mocks before each test | ||
jest.clearAllMocks(); | ||
|
||
// Mock the Strapi object | ||
strapiMock = { | ||
db: { | ||
lifecycles: { | ||
subscribe: jest.fn(), | ||
}, | ||
baitUUID: {}, | ||
}, | ||
}, | ||
'api::secondTestModel': { | ||
attributes: { | ||
title: {}, | ||
uid: {}, | ||
}, | ||
}, | ||
'api::thirdTestModel': { | ||
attributes: { | ||
actualUUID: { | ||
customField: "plugin::strapi-advanced-uuid.uuid", | ||
contentTypes: { | ||
'api::article.article': { | ||
attributes: { | ||
uuidField: { | ||
customField: 'plugin::strapi-advanced-uuid.uuid', | ||
options: { 'uuid-format': '^[A-Za-z0-9]{5}$', 'disable-auto-fill': false }, | ||
}, | ||
title: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
'api::product.product': { | ||
attributes: { | ||
sku: { | ||
customField: 'plugin::strapi-advanced-uuid.uuid', | ||
options: { 'uuid-format': '^[0-9a-zA-Z-]{8}$', 'disable-auto-fill': false }, | ||
}, | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
// Call the bootstrap method to set up lifecycle hooks | ||
bootstrap({ strapi: strapiMock }); | ||
}); | ||
|
||
describe("bootstrap", () => { | ||
it("should subscribe to lifecycles for models that have uuid fields", () => { | ||
bootstrap({ strapi }); | ||
test('should subscribe to beforeCreate and beforeUpdate hooks', () => { | ||
// Ensure the subscribe method is called | ||
expect(strapiMock.db.lifecycles.subscribe).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
models: expect.arrayContaining(['api::article.article', 'api::product.product']), | ||
beforeCreate: expect.any(Function), | ||
beforeUpdate: expect.any(Function), | ||
}) | ||
); | ||
}); | ||
|
||
expect(strapi.db.lifecycles.subscribe).toHaveBeenCalledTimes(1); | ||
expect(strapi.db.lifecycles.subscribe).toHaveBeenCalledWith(expect.any(Function)); | ||
test('beforeCreate generates UUID for article if not provided', () => { | ||
// Extract the beforeCreate hook | ||
const lifecycleHook = strapiMock.db.lifecycles.subscribe.mock.calls[0][0].beforeCreate; | ||
|
||
const uuid = v4() | ||
const subscribeCallback = strapi.db.lifecycles.subscribe.mock.calls[0][0]; | ||
// Mock the event for creating an article | ||
const event = { | ||
action: "beforeCreate", | ||
model: { | ||
uid: "api::firstTestModel", | ||
}, | ||
params: { | ||
data: { | ||
baitUUID: "invalid-uuid", | ||
actualUUID: uuid, | ||
}, | ||
}, | ||
model: strapiMock.contentTypes['api::article.article'], | ||
params: { data: { title: 'New Article' } }, // uuidField not provided | ||
}; | ||
subscribeCallback(event); | ||
|
||
expect(event.params.data.baitUUID).toBe("invalid-uuid"); | ||
expect(validate(event.params.data.actualUUID)).toBe(true); | ||
expect(event.params.data.actualUUID).toBe(uuid); | ||
}); | ||
// Invoke the lifecycle hook | ||
lifecycleHook(event); | ||
|
||
it("should not subscribe to lifecycles for models that don't have uuid fields", () => { | ||
bootstrap({ strapi }); | ||
// Assert that UUID is generated and matches the expected format | ||
expect(event.params.data.uuidField).toMatch(/^[A-Za-z0-9]{5}$/); | ||
}); | ||
|
||
expect(strapi.db.lifecycles.subscribe).toHaveBeenCalledTimes(1); | ||
expect(strapi.db.lifecycles.subscribe).toHaveBeenCalledWith(expect.any(Function)); | ||
test('beforeCreate validates SKU format for product', () => { | ||
// Extract the beforeCreate hook | ||
const lifecycleHook = strapiMock.db.lifecycles.subscribe.mock.calls[0][0].beforeCreate; | ||
|
||
const subscribeCallback = strapi.db.lifecycles.subscribe.mock.calls[0][0]; | ||
// Mock the event for creating a product with an invalid SKU | ||
const event = { | ||
action: "beforeCreate", | ||
model: { | ||
uid: "api::secondTestModel", | ||
}, | ||
params: { | ||
data: { | ||
title: "name", | ||
uid: "another-uid" | ||
model: strapiMock.contentTypes['api::product.product'], | ||
params: { data: { sku: 'invalidsku' } }, // Doesn't match format ^[0-9a-zA-Z-]{8}$ | ||
}; | ||
|
||
// Invoke the lifecycle hook | ||
lifecycleHook(event); | ||
|
||
// Assert that handleYupError is called due to invalid SKU format | ||
expect(handleYupError).toHaveBeenCalledWith( | ||
expect.objectContaining({ inner: expect.any(Array) }), | ||
'You have some issues' | ||
); | ||
}); | ||
|
||
test('beforeCreate does not auto-generate UUID if disableAutoFill is true', () => { | ||
// Mock model with disableAutoFill set to true | ||
const userModel = { | ||
attributes: { | ||
userId: { | ||
customField: 'plugin::strapi-advanced-uuid.uuid', | ||
options: { 'uuid-format': '^[0-9]{6}$', 'disable-auto-fill': true }, | ||
}, | ||
username: { | ||
type: 'string', | ||
}, | ||
}, | ||
}; | ||
subscribeCallback(event); | ||
|
||
expect(event.params.data.title).toBe("name"); | ||
expect(event.params.data.uid).toBe("another-uid"); | ||
}); | ||
// Update strapiMock to add the userModel | ||
strapiMock.contentTypes['api::user.user'] = userModel; | ||
|
||
it("should generate v4 uuid for empty uuid fields", () => { | ||
bootstrap({ strapi }); | ||
// Call the bootstrap method to update lifecycle hooks | ||
bootstrap({ strapi: strapiMock }); | ||
|
||
expect(strapi.db.lifecycles.subscribe).toHaveBeenCalledTimes(1); | ||
expect(strapi.db.lifecycles.subscribe).toHaveBeenCalledWith(expect.any(Function)); | ||
// Extract the beforeCreate hook | ||
const lifecycleHook = strapiMock.db.lifecycles.subscribe.mock.calls[1][0].beforeCreate; | ||
|
||
const subscribeCallback = strapi.db.lifecycles.subscribe.mock.calls[0][0]; | ||
// Mock the event for creating a user | ||
const event = { | ||
action: "beforeCreate", | ||
model: { | ||
uid: "api::thirdTestModel", | ||
}, | ||
params: { | ||
data: { | ||
actualUUID: "" | ||
}, | ||
}, | ||
model: userModel, | ||
params: { data: { username: 'testuser' } }, // userId not provided | ||
}; | ||
subscribeCallback(event); | ||
expect(validate(event.params.data.actualUUID)).toBe(true); | ||
|
||
// Invoke the lifecycle hook | ||
lifecycleHook(event); | ||
|
||
// Assert that userId is not generated | ||
expect(event.params.data.userId).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.