summaryrefslogtreecommitdiff
path: root/spec/helpers/time_helper_spec.rb
blob: 3e406f5e74e36abf477c4847c84e00cebc80dd9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe TimeHelper do
  describe "#time_interval_in_words" do
    it "returns minutes and seconds" do
      intervals_in_words = {
        60 => "1 minute",
        100 => "1 minute and 40 seconds",
        100.32 => "1 minute and 40 seconds",
        120 => "2 minutes",
        121 => "2 minutes and 1 second",
        3721 => "62 minutes and 1 second",
        0 => "0 seconds"
      }

      intervals_in_words.each do |interval, expectation|
        expect(time_interval_in_words(interval)).to eq(expectation)
      end
    end
  end

  describe "#duration_in_numbers" do
    using RSpec::Parameterized::TableSyntax

    where(:duration, :formatted_string) do
      0                              | "00:00"
      1.second                       | "00:01"
      42.seconds                     | "00:42"
      2.minutes + 1.second           | "02:01"
      3.hours + 2.minutes + 1.second | "03:02:01"
      30.hours                       | "30:00:00"
    end

    with_them do
      it { expect(duration_in_numbers(duration)).to eq formatted_string }
    end
  end

  describe "#time_in_milliseconds" do
    it "returns the time in milliseconds" do
      freeze_time do
        time = (Time.now.to_f * 1000).to_i

        expect(time_in_milliseconds).to eq time
      end
    end
  end
end