Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlebkowski/2f55105c348225ca18be7364d461ea1e to your computer and use it in GitHub Desktop.
Save mlebkowski/2f55105c348225ca18be7364d461ea1e to your computer and use it in GitHub Desktop.
describe('ConnectionIdentifierLabelFactory', () => {
// test cases declarations using gherkin syntax:
test('call pstn number', () => {
when_i_call('+48500100100');
then_i_expect_label_to_be('+48 500 100 100');
});
test('call task by id with prefix', () => {
const id = uuid();
given_there_is_a_task(new Task(id, '+48500100100'));
when_i_call(`task:${id}`);
then_i_expect_label_to_be('+48 500 100 100');
});
// scenario context preparation with defaults
// dependencies:
let workstationRepository: WorkstationRepository;
let taskRepository: TaskRepository;
let patientRepository: PatientRepository;
// result:
let label: string;
// reset to some defaults before each scenario:
beforeEach(() => {
label = '';
workstationRepository = new WorkstationRepositoryStub();
taskRepository = new TaskRepositoryStub();
patientRepository = new PatientRepositoryStub();
});
// region steps definitions: givens, whens and thens
function given_there_is_a_task(...tasks: Task[]): void {
taskRepository = new TaskRepositoryStub(tasks);
}
// create system under test with all the prepared dependencies
// and calculate the result for given input
function when_i_call(to: string) {
const sut = new ConnectionIdentifierLabelFactory(
workstationRepository,
taskRepository,
patientRepository
);
label = sut.get(to);
}
// assert that the results match expectations
function then_i_expect_label_to_be(expected: string) {
expect(label).toBe(expected);
}
});
export default class ConnectionIdentifierLabelFactory {
constructor(
private readonly workstations: WorkstationRepository,
private readonly tasks: TaskRepository,
private readonly patients: PatientRepository
) {}
get(to: string): string {
const patient = this.patients.get(to);
if (patient) {
return patient.displayName;
}
const workstation = this.workstations.find(to);
if (workstation) {
return workstation.displayName;
}
const task = this.tasks.find(to);
if (task) {
return task.displayName(this.patients);
}
return phoneNumberFormatter(to);
}
}
// 1. Start out with a class having a global dependency
class GlobalDependencyApiClient {
items(page: number) {
return (new HttpClient).get(`/items?page=${page}`);
}
}
// 2. Expect the dependency from the outside
class DependencyInversionApiClient {
constructor(private readonly http: HttpClient) {}
items(page: number) {
return this.http.get(`/items?page=${page}`);
}
}
// 3. Extract the logic to a function
function ItemsFetcher(http: HttpClient, page: number) {
return http.get(`/items?page=${page}`);
}
class ApiClient {
constructor(private readonly http: HttpClient) {}
items(page: number) {
return ItemsFetcher(this.http, page);
}
}
// inject as an argument and use partial application to provide the dependency
const FunctionalApiApiClient = ItemsFetcher.bind(null, new HttpClient);
// inject using the constructor
const ObjectOrientedApiClient = new ApiClient(new HttpClient);
export const authorization = <Authorization>resolve(Authorization);
export type { Authorization };
export { default as BookVisit } from './ui/BookVisitProvider.vue';
export default class ServiceMother {
static getSome(): Service {
return new Service(numberId(), 'consultation online', numberId());
}
static getWithAddressId(addressId: string): Service {
return new Service(numberId(), 'consultation online', addressId);
}
}
describe('LoadMoreEvaluator', () => {
test.each`
numberOfBookedSlots | numberOfFreeSlots | expected
${80} | ${80} | ${true}
${300} | ${80} | ${true}
${30} | ${101} | ${false}
${120} | ${120} | ${false}
`(
'should return $expected if there is $numberOfBookedSlots booked slots and $numberOfFreeSlots free slots',
({ numberOfBookedSlots, numberOfFreeSlots, expected }) => {
const bookingSlots = BookingSlotsMother.createWith(numberOfFreeSlots, numberOfBookedSlots);
const sut = new LoadMoreEvaluator();
expect(sut.shouldLoadMore(bookingSlots)).toBe(expected);
}
);
});
const fileRepository = {
uploadPublicFile: jest.fn(),
};
describe('MediaFileUploader', () => {
beforeEach(() => {
fileRepository.uploadPublicFile.mockClear();
});
test('should not upload empty file', async () => {
const sut = new MediaFileUploader(fileRepository);
await sut.tryUpload(null);
expect(fileRepository.uploadPublicFile).not.toHaveBeenCalled();
});
}
export default class SsoProvider implements DpProvider {
register(container: DependencyContainer): void {
container.register<SsoFlowRepository>(SsoFlowRepositoryToken, SessionStorageSsoFlowRepository);
container.register<SsoFlowInitializer>(SsoFlowInitializer, {
useFactory: (c: DependencyContainer) =>
new SsoFlowInitializer(c.resolve(SsoFlowRepositoryToken), window),
});
}
}
export default class StaticSsoFlowRepository implements SsoFlowRepository {
readonly storage = new Map();
readonly tokens: string[] = [];
get(): never {
throw new Error('Method not implemented.');
}
save(token: string, value: SsoFlowState): void {
this.tokens.push(token);
this.storage.set(token, value);
}
}
@injectable()
export default class StorePatientAdapter implements PatientRepository {
constructor(
@inject(RootStoreToken)
private readonly store: Store,
private readonly patientFactory: PatientFactory
) {}
async searchQuery(phrase?: string): Promise<Patient[]> {
const { items } = await this.store.dispatch(
FETCH_PATIENTS_BY_QUERY_ACTION,
this.getQueryParams(phrase)
);
// map DTO to entities:
return items.map(this.patientFactory.make);
}
}
export default class TaskTileDisplayReminderBadge extends Vue {
@Prop({ type: Object, required: true })
readonly task: Task;
private readonly reminderFactory: ReminderFactory = resolve(ReminderFactory);
private readonly dateConverter: DateConverter = resolve(DateConverterToken);
get date(): DateInterface {
return {
formatted: this.reminder.getFormattedDate(this.dateConverter),
};
}
private get reminder(): Reminder {
return this.reminderFactory.make(this.task.id, this.task.reminderAt);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment