---
# System prepended metadata

title: Availability Tests

---

# Availability Tests

Source gist: https://gist.github.com/n8io/8b2b8f3cd30c95e6aa64ba1b1881b63b

```typescript=
import { DayOfWeek, WorkingHours } from '@microsoft/microsoft-graph-types'
import { AvailabilityViewToAvailabilityViewSansWorkingHoursParameters, availabilityViewToAvailabilityViewSansWorkingHours } from '.'
import { ScheduleItemAvailability } from '../../models'

const { BUSY, FREE } = ScheduleItemAvailability

describe('availabilityViewToAvailabilityViewSansWorkingHours', () => {
  const baseInput: AvailabilityViewToAvailabilityViewSansWorkingHoursParameters = {
    durationInMinutes: 60,
    startDate: new Date('2022-01-02T05:01Z'), // Sunday Jan 1st, 12:01am EST (The '01' is used verify we're rounding to the nearest duration)
    view: undefined,
    workingHours: {
      timeZone: { name: 'Eastern Standard Time' },
    },
  }

  const freeAllWeek = FREE.repeat(24 * 7)
  const mondayThruFriday: DayOfWeek[] = ['monday', 'tuesday', 'tuesday', 'wednesday', 'thursday', 'friday']

  describe('when given no work hours', () => {
    const workingHours: WorkingHours | undefined = undefined

    test('returns unmodified blocks', () => {
      const view = 'VIEW_UNCHANGED'

      const actual = availabilityViewToAvailabilityViewSansWorkingHours({
        ...baseInput,
        view,
        // @ts-expect-error we're passing undefined on purpose
        workingHours,
      })

      expect(actual).toEqual(view)
    })
  })

  describe('when given empty work hours', () => {
    const workingHours: WorkingHours = {}

    test('returns unmodified blocks', () => {
      const view = 'VIEW_UNCHANGED'

      const actual = availabilityViewToAvailabilityViewSansWorkingHours({
        ...baseInput,
        view,
        workingHours,
      })

      expect(actual).toEqual(view)
    })
  })

  describe('when given valid working hours', () => {
    describe('and work days and hours are restricted', () => {
      test('return only the restricted work days and work hours', () => {
        const actual = availabilityViewToAvailabilityViewSansWorkingHours({
          ...baseInput,
          view: freeAllWeek,
          workingHours: {
            ...baseInput.workingHours,
            daysOfWeek: mondayThruFriday,
            startTime: '09:00:00',
            endTime: '17:00:00',
          },
        })

        const weekend = BUSY.repeat(24)
        const weekday = [BUSY.repeat(9), FREE.repeat(8), BUSY.repeat(7)].join('')

        const dollyPartonHoursView = [
          // Only working weekdays, 9a-5p
          weekend,
          weekday.repeat(5),
          weekend,
        ].join('')

        expect(actual).toEqual(dollyPartonHoursView)
      })
    })

    describe('and work days are NOT restricted', () => {
      const daysOfWeek: DayOfWeek[] = []

      test('returns every day restricted by working hours', () => {
        const actual = availabilityViewToAvailabilityViewSansWorkingHours({
          ...baseInput,
          view: freeAllWeek,
          workingHours: {
            ...baseInput.workingHours,
            daysOfWeek,
            startTime: '09:00:00',
            endTime: '17:00:00',
          },
        })

        const workday = [BUSY.repeat(9), FREE.repeat(8), BUSY.repeat(7)].join('')

        // Every day 9a-5p
        const expected = workday.repeat(7)

        expect(actual).toEqual(expected)
      })
    })

    describe('and only work start hour is restricted', () => {
      test('returns only the work hours after the start time', () => {
        const actual = availabilityViewToAvailabilityViewSansWorkingHours({
          ...baseInput,
          view: freeAllWeek,
          workingHours: {
            ...baseInput.workingHours,
            startTime: '09:00:00',
            endTime: undefined,
          },
        })

        const workday = [BUSY.repeat(9), FREE.repeat(15)].join('')

        // Every day after 9a
        const expected = workday.repeat(7)

        expect(actual).toEqual(expected)
      })
    })

    describe('and only work end hour is restricted', () => {
      test('returns only the hours before the end time', () => {
        const actual = availabilityViewToAvailabilityViewSansWorkingHours({
          ...baseInput,
          view: freeAllWeek,
          workingHours: {
            ...baseInput.workingHours,
            startTime: undefined,
            endTime: '17:00:00',
          },
        })

        const workday = [FREE.repeat(17), BUSY.repeat(7)].join('')

        // Every day before 5p
        const expected = workday.repeat(7)

        expect(actual).toEqual(expected)
      })
    })

    describe('and work hours are not restricted', () => {
      test('returns only the time blocks on work days', () => {
        const actual = availabilityViewToAvailabilityViewSansWorkingHours({
          ...baseInput,
          view: freeAllWeek,
          workingHours: {
            ...baseInput.workingHours,
            daysOfWeek: mondayThruFriday,
            startTime: undefined,
            endTime: undefined,
          },
        })

        const weekend = BUSY.repeat(24) // Busy all day
        const weekday = FREE.repeat(24) // Free all day

        const expected = [
          // Every weekday day, all day
          weekend,
          weekday.repeat(5),
          weekend,
        ].join('')

        expect(actual).toEqual(expected)
      })
    })
  })

  describe('when the view has non-availabilities', () => {
    const busyFrom9aTo5p = [FREE.repeat(9), BUSY.repeat(8), FREE.repeat(7)].join('')

    const workingHours: WorkingHours = {
      ...baseInput.workingHours,
      daysOfWeek: ['sunday'],
      startTime: '09:00:00',
      endTime: '17:00:00',
    }

    test('view non-availabilities supersede working hour availabilities', () => {
      const actual = availabilityViewToAvailabilityViewSansWorkingHours({
        ...baseInput,
        view: busyFrom9aTo5p,
        workingHours,
      })

      const expected = BUSY.repeat(busyFrom9aTo5p.length)

      expect(actual).toEqual(expected)
    })
  })

  describe('when the start date is not the beginning of the day or week', () => {
    const startDate = new Date('2022-01-05T10:00Z')

    const workingHours: WorkingHours = {
      ...baseInput.workingHours,
      daysOfWeek: [],
      startTime: '11:00:00',
      endTime: '13:00:00',
    }

    test('view non-availabilities supersede working hour availabilities', () => {
      const actual = availabilityViewToAvailabilityViewSansWorkingHours({
        ...baseInput,
        startDate,
        view: '0220',
        workingHours,
      })

      expect(actual).toEqual('2222')
    })
  })

  describe.only('when the duration is 15 minutes', () => {
    const durationInMinutes = 15

    test('view non-availabilities supersede working hour availabilities', () => {
      const freeAllWeek15MinBlocks = FREE.repeat(7 * 24 * (60 / durationInMinutes))

      const actual = availabilityViewToAvailabilityViewSansWorkingHours({
        ...baseInput,
        durationInMinutes,
        view: freeAllWeek15MinBlocks,
        workingHours: {
          ...baseInput.workingHours,
          daysOfWeek: mondayThruFriday,
          startTime: '09:00:00',
          endTime: '17:00:00',
        },
      })

      const weekend = BUSY.repeat(96) // Busy all day
      const workday = [BUSY.repeat(36), FREE.repeat(32), BUSY.repeat(28)].join('') // Free all day

      const expected = [
        // Every weekday day, all day
        weekend,
        workday.repeat(5),
        weekend,
      ].join('')

      expect(actual).toEqual(expected)
    })
  })
})

```