-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
311 additions
and
89 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 |
---|---|---|
|
@@ -26,3 +26,6 @@ jobs: | |
|
||
- name: Build | ||
run: yarn build | ||
|
||
- name: Unit tests | ||
run: yarn test:unit |
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 |
---|---|---|
|
@@ -90,9 +90,10 @@ | |
"hooks:uninstall": "husky uninstall", | ||
"hooks:install": "husky install", | ||
"cypress:open": "env-cmd -f ./.env.test cypress open --browser chrome", | ||
"test": "yarn build:test && concurrently -k -s first \"yarn preview:test\" \"yarn cypress:run\"", | ||
"test": "yarn test:unit && yarn build:test && concurrently -k -s first \"yarn preview:test\" \"yarn cypress:run\"", | ||
"cypress:run": "env-cmd -f ./.env.test cypress run --browser chrome", | ||
"postinstall": "husky install" | ||
"postinstall": "husky install", | ||
"test:unit": "yarn vitest" | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
|
@@ -148,7 +149,8 @@ | |
"typescript": "5.3.3", | ||
"vite": "5.1.4", | ||
"vite-plugin-checker": "0.6.4", | ||
"vite-plugin-istanbul": "5.0.0" | ||
"vite-plugin-istanbul": "5.0.0", | ||
"vitest": "1.4.0" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { DiscriminatedItem, PermissionLevel } from '@graasp/sdk'; | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { | ||
getHighestPermissionForMemberFromMemberships, | ||
getParentsIdsFromPath, | ||
isUrlValid, | ||
transformIdForPath, | ||
} from './item'; | ||
|
||
describe('item utils', () => { | ||
it('isUrlValid', () => { | ||
expect(isUrlValid(null)).toBeFalsy(); | ||
expect(isUrlValid()).toBeFalsy(); | ||
expect(isUrlValid('somelink')).toBeFalsy(); | ||
expect(isUrlValid('graasp.eu')).toBeTruthy(); | ||
expect(isUrlValid('https://graasp')).toBeFalsy(); | ||
|
||
expect(isUrlValid('https://graasp.eu')).toBeTruthy(); | ||
expect(isUrlValid('http://graasp.eu')).toBeTruthy(); | ||
expect(isUrlValid('https://www.youtube.com/')).toBeTruthy(); | ||
}); | ||
|
||
it('transformIdForPath', () => { | ||
expect(transformIdForPath('someid')).toEqual('someid'); | ||
expect(transformIdForPath('some-id')).toEqual('some_id'); | ||
const id = 'ecafbd2a-5688-11eb-ae93-0242ac130002'; | ||
const path = 'ecafbd2a_5688_11eb_ae93_0242ac130002'; | ||
expect(transformIdForPath(id)).toEqual(path); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
expect(() => transformIdForPath(null)).toThrow(); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
expect(() => transformIdForPath(undefined)).toThrow(); | ||
}); | ||
|
||
describe('getParentsIdsFromPath', () => { | ||
it('default', () => { | ||
expect(getParentsIdsFromPath('someid')).toEqual(['someid']); | ||
expect(getParentsIdsFromPath('parent.child')).toEqual([ | ||
'parent', | ||
'child', | ||
]); | ||
expect( | ||
getParentsIdsFromPath( | ||
'ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001', | ||
), | ||
).toEqual([ | ||
'ecafbd2a-5688-11eb-ae93-0242ac130002', | ||
'ecafbd2a-5688-11eb-ae93-0242ac130001', | ||
]); | ||
expect( | ||
getParentsIdsFromPath( | ||
'ecafbd2a_5688_11eb_ae93_0242ac130003.ecafbd2a_5688_11eb_ae93_0242ac130004.ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001', | ||
), | ||
).toEqual([ | ||
'ecafbd2a-5688-11eb-ae93-0242ac130003', | ||
'ecafbd2a-5688-11eb-ae93-0242ac130004', | ||
'ecafbd2a-5688-11eb-ae93-0242ac130002', | ||
'ecafbd2a-5688-11eb-ae93-0242ac130001', | ||
]); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
expect(getParentsIdsFromPath(null)).toEqual([]); | ||
expect(getParentsIdsFromPath(undefined)).toEqual([]); | ||
}); | ||
it('ignoreSelf = true', () => { | ||
expect(getParentsIdsFromPath('someid', { ignoreSelf: true })).toEqual([]); | ||
expect( | ||
getParentsIdsFromPath('parent.child', { ignoreSelf: true }), | ||
).toEqual(['parent']); | ||
expect( | ||
getParentsIdsFromPath( | ||
'ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001', | ||
{ ignoreSelf: true }, | ||
), | ||
).toEqual(['ecafbd2a-5688-11eb-ae93-0242ac130002']); | ||
expect( | ||
getParentsIdsFromPath( | ||
'ecafbd2a_5688_11eb_ae93_0242ac130003.ecafbd2a_5688_11eb_ae93_0242ac130004.ecafbd2a_5688_11eb_ae93_0242ac130002.ecafbd2a_5688_11eb_ae93_0242ac130001', | ||
{ ignoreSelf: true }, | ||
), | ||
).toEqual([ | ||
'ecafbd2a-5688-11eb-ae93-0242ac130003', | ||
'ecafbd2a-5688-11eb-ae93-0242ac130004', | ||
'ecafbd2a-5688-11eb-ae93-0242ac130002', | ||
]); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
expect(getParentsIdsFromPath(null, { ignoreSelf: true })).toEqual([]); | ||
expect(getParentsIdsFromPath(undefined, { ignoreSelf: true })).toEqual( | ||
[], | ||
); | ||
}); | ||
}); | ||
|
||
describe('getHighestPermission', () => { | ||
it('returns null when no memberId', () => { | ||
expect( | ||
getHighestPermissionForMemberFromMemberships({ | ||
memberId: undefined, | ||
itemPath: '1234', | ||
}), | ||
).toEqual(null); | ||
}); | ||
|
||
it('returns null when no memberships', () => { | ||
expect( | ||
getHighestPermissionForMemberFromMemberships({ | ||
memberId: '1234', | ||
itemPath: '1234', | ||
memberships: undefined, | ||
}), | ||
).toEqual(null); | ||
expect( | ||
getHighestPermissionForMemberFromMemberships({ | ||
memberId: '1234', | ||
itemPath: '1234', | ||
memberships: [], | ||
}), | ||
).toEqual(null); | ||
}); | ||
|
||
it('returns closest permission when only one permission', () => { | ||
const member = { id: '1234', name: 'bob', email: '[email protected]' }; | ||
const item = { path: '1234' } as DiscriminatedItem; | ||
const membership = { | ||
id: 'membership-123', | ||
member, | ||
item, | ||
permission: PermissionLevel.Read, | ||
creator: null, | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
expect( | ||
getHighestPermissionForMemberFromMemberships({ | ||
memberId: member.id, | ||
itemPath: item.path, | ||
memberships: [membership], | ||
}), | ||
).toEqual(membership); | ||
}); | ||
|
||
it('returns closest permission when multiple permissions', () => { | ||
const member = { id: '1234', name: 'bob', email: '[email protected]' }; | ||
const item1 = { path: '1234' } as DiscriminatedItem; | ||
const item2 = { path: '1234.5678' } as DiscriminatedItem; | ||
const membership1 = { | ||
id: 'membership-123', | ||
member, | ||
item: item1, | ||
permission: PermissionLevel.Read, | ||
creator: null, | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
const membership2 = { | ||
id: 'membership-123', | ||
member, | ||
item: item2, | ||
permission: PermissionLevel.Read, | ||
creator: null, | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
expect( | ||
getHighestPermissionForMemberFromMemberships({ | ||
memberId: member.id, | ||
itemPath: item2.path, | ||
memberships: [membership1, membership2], | ||
}), | ||
).toEqual(membership2); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.