-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
2 changed files
with
139 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import type { AccountID } from '../../accounts/model/account.js'; | ||
import { type CreateListArgs, List, type ListID } from './list.js'; | ||
|
||
describe('List', () => { | ||
const args: CreateListArgs = { | ||
id: '1' as ListID, | ||
title: 'My List', | ||
publicity: 'PUBLIC', | ||
ownerId: '2' as AccountID, | ||
memberIds: ['3' as AccountID], | ||
createdAt: new Date(), | ||
} as const; | ||
|
||
it('should create a new list', () => { | ||
const list = List.new(args); | ||
|
||
expect(list.getId()).toBe(args.id); | ||
expect(list.getTitle()).toBe(args.title); | ||
expect(list.isPublic()).toBe(true); | ||
expect(list.getOwnerId()).toBe(args.ownerId); | ||
expect(list.getMemberIds()).toEqual(args.memberIds); | ||
expect(list.getCreatedAt()).toBe(args.createdAt); | ||
}); | ||
|
||
it('should add a member to the list', () => { | ||
const list = List.new(args); | ||
const memberId = '4' as AccountID; | ||
|
||
list.addMember(memberId); | ||
|
||
expect(list.getMemberIds()).toStrictEqual([ | ||
'3' as AccountID, | ||
'4' as AccountID, | ||
]); | ||
}); | ||
|
||
it('should not add a member if already in the list', () => { | ||
const args: CreateListArgs = { | ||
id: '1' as ListID, | ||
title: 'My List', | ||
publicity: 'PUBLIC', | ||
ownerId: '2' as AccountID, | ||
memberIds: ['3' as AccountID], | ||
createdAt: new Date(), | ||
} as const; | ||
const list3 = List.new(args); | ||
const memberId = '3' as AccountID; | ||
|
||
list3.addMember(memberId); | ||
|
||
expect(list3.getMemberIds()).toStrictEqual(['3' as AccountID]); | ||
}); | ||
|
||
it('should remove member from list', () => { | ||
const args: CreateListArgs = { | ||
id: '1' as ListID, | ||
title: 'My List', | ||
publicity: 'PUBLIC', | ||
ownerId: '2' as AccountID, | ||
memberIds: ['3' as AccountID], | ||
createdAt: new Date(), | ||
} as const; | ||
const list3 = List.new(args); | ||
const memberId = '3' as AccountID; | ||
|
||
list3.removeMember(memberId); | ||
|
||
expect(list3.getMemberIds()).toStrictEqual([]); | ||
}); | ||
}); |
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,67 @@ | ||
import type { AccountID } from '../../accounts/model/account.js'; | ||
import type { ID } from '../../id/type.js'; | ||
|
||
export type ListID = ID<List>; | ||
export interface CreateListArgs { | ||
id: ListID; | ||
title: string; | ||
publicity: 'PUBLIC' | 'PRIVATE'; | ||
ownerId: AccountID; | ||
memberIds: AccountID[]; | ||
createdAt: Date; | ||
} | ||
export class List { | ||
private readonly id: ListID; | ||
private readonly title: string; | ||
private readonly publicity: 'PUBLIC' | 'PRIVATE'; | ||
private readonly ownerId: AccountID; | ||
private memberIds: AccountID[]; | ||
private readonly createdAt: Date; | ||
|
||
private constructor(args: CreateListArgs) { | ||
this.id = args.id; | ||
this.title = args.title; | ||
this.publicity = args.publicity; | ||
this.ownerId = args.ownerId; | ||
this.memberIds = args.memberIds; | ||
this.createdAt = args.createdAt; | ||
} | ||
|
||
static new(args: CreateListArgs) { | ||
return new List(args); | ||
} | ||
|
||
getId(): ListID { | ||
return this.id; | ||
} | ||
|
||
getTitle(): string { | ||
return this.title; | ||
} | ||
|
||
isPublic(): boolean { | ||
return this.publicity === 'PUBLIC'; | ||
} | ||
|
||
getOwnerId(): AccountID { | ||
return this.ownerId; | ||
} | ||
|
||
getMemberIds(): AccountID[] { | ||
return this.memberIds; | ||
} | ||
|
||
getCreatedAt(): Date { | ||
return this.createdAt; | ||
} | ||
|
||
addMember(memberId: AccountID) { | ||
// ToDo: member limit | ||
if (this.memberIds.includes(memberId)) return; | ||
this.memberIds.push(memberId); | ||
} | ||
|
||
removeMember(memberId: AccountID) { | ||
this.memberIds = this.memberIds.filter((id) => id !== memberId); | ||
} | ||
} |