Skip to content

Instantly share code, notes, and snippets.

@ikotse-code
Last active January 4, 2026 20:09
Show Gist options
  • Select an option

  • Save ikotse-code/f6c2b49891734a225dffd18d0f9fd7a2 to your computer and use it in GitHub Desktop.

Select an option

Save ikotse-code/f6c2b49891734a225dffd18d0f9fd7a2 to your computer and use it in GitHub Desktop.
6
import { User } from '../src/user';
test("should be able to give consent",() => {
const user = new User( "Will", "Doe", 19);
user.giveConsent();
expect(user.getConsent()).toBeTruthy();
})
test("should not be able to give consent",() => {
const user = new User( "Jane", "Doe", 18);
user.giveConsent();
expect(user.getConsent()).toBeFalsy();
})
test("revoke consent",() => {
const user = new User( "John", "Doe", 19);
user.giveConsent();
expect(user.getConsent()).toBeTruthy();
user.revokeConsent();
expect(user.getConsent()).toBeFalsy();
})
test("consent is not given by default",() => {
const user = new User( "John", "Doe", 19);
expect(user.getConsent()).toBeFalsy();
user.revokeConsent();
expect(user.getConsent()).toBeFalsy();
})
test("consent is not given by default",() => {
const user = new User( "Derek", "Doe", 18);
expect(user.getConsent()).toBeFalsy();
expect(user.name).toBe("Derek");
})
test("should be adult",() => {
const user = new User( "Will", "Doe", 19);
expect(user.isAdult()).toBeTruthy();
})
test("should be adult, age validation",() => {
const user = new User( "Will", "Doe", -19);
user.setAge(user.age);
console.log(user.age);
expect(user.age).toBe(0);
expect(user.isAdult()).toBeFalsy();
user.setAge(-5);
console.log(user.age);
expect(user.age).toBe(0);
user.setAge(0);
console.log(user.age);
expect(user.age).toBe(0);
user.setAge(20);
console.log(user.age);
expect(user.age).toBe(20);
expect(user.isAdult()).toBeTruthy();
})
export class User{
name: string;
surname:string;
isConsentGiven:boolean;
age:number;
constructor(name: string, surname: string, age: number) {
this.name = name;
this.surname = surname;
this.isConsentGiven = false; // by default
this.age = age;
//this.age = 18; // by default
}
giveConsent(): boolean {
let isAgeCondition: boolean = this.age > 18;
if (isAgeCondition) {
return this.isConsentGiven = true;
} else {
return this.isConsentGiven = false;
}
}
revokeConsent() {
this.isConsentGiven = false;
}
// giveConsent() {
// this.consentGiven = true;
// }
getConsent(): boolean {
return this.isConsentGiven;
}
isAdult(): boolean {
return this.age >= 18;
}
setAge(age: number): number {
let isAge: boolean = age > 0;
if (isAge) {
return this.age = age;
} else {
return this.age = 0;
}
}
// setAge(age: number) {
// this.age = age;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment