FIX: improves code, tests and utc handling of local-dates (#6644)

This commit is contained in:
Joffrey JAFFEUX
2018-11-22 17:19:24 +01:00
committed by GitHub
parent 56478166e5
commit 3ff3bb6e2a
8 changed files with 435 additions and 96 deletions

View File

@ -2,13 +2,13 @@ require 'rails_helper'
RSpec.describe "Local Dates" do
before do
freeze_time
freeze_time DateTime.parse('2018-11-10 12:00')
end
it "should work without timezone" do
post = Fabricate(:post, raw: <<~SQL)
post = Fabricate(:post, raw: <<~TXT)
[date=2018-05-08 time=22:00 format="L LTS" timezones="Europe/Paris|America/Los_Angeles"]
SQL
TXT
cooked = post.cooked
@ -26,9 +26,9 @@ RSpec.describe "Local Dates" do
end
it "should work with timezone" do
post = Fabricate(:post, raw: <<~SQL)
post = Fabricate(:post, raw: <<~TXT)
[date=2018-05-08 time=22:00 format="L LTS" timezone="Asia/Calcutta" timezones="Europe/Paris|America/Los_Angeles"]
SQL
TXT
cooked = post.cooked
@ -37,13 +37,44 @@ RSpec.describe "Local Dates" do
end
it 'requires the right attributes to convert to a local date' do
post = Fabricate(:post, raw: <<~SQL)
post = Fabricate(:post, raw: <<~TXT)
[date]
SQL
TXT
cooked = post.cooked
expect(post.cooked).to include("<p>[date]</p>")
expect(cooked).to_not include('data-date=')
end
it 'requires the right attributes to convert to a local date' do
post = Fabricate(:post, raw: <<~TXT)
[date]
TXT
cooked = post.cooked
expect(post.cooked).to include("<p>[date]</p>")
expect(cooked).to_not include('data-date=')
end
it 'it works with only a date and time' do
raw = "[date=2018-11-01 time=12:00]"
cooked = Fabricate(:post, raw: raw).cooked
expect(cooked).to include('data-date="2018-11-01"')
expect(cooked).to include('data-time="12:00"')
end
it 'doesn’t include format by default' do
raw = "[date=2018-11-01 time=12:00]"
cooked = Fabricate(:post, raw: raw).cooked
expect(cooked).not_to include('data-format=')
end
it 'doesn’t include timezone by default' do
raw = "[date=2018-11-01 time=12:00]"
cooked = Fabricate(:post, raw: raw).cooked
expect(cooked).not_to include("data-timezone=")
end
end

View File

@ -1,24 +1,52 @@
require 'rails_helper'
def generate_html(text, opts = {})
output = "<p><span class=\"discourse-local-date\""
output += " data-date=\"#{opts[:date]}\"" if opts[:date]
output += " data-time=\"#{opts[:time]}\"" if opts[:time]
output += " data-format=\"#{opts[:format]}\"" if opts[:format]
output += " data-email-preview=\"#{opts[:email_preview]}\"" if opts[:email_preview]
output += ">"
output += text
output + "</span></p>"
end
describe PrettyText do
it 'uses a simplified syntax in emails' do
before do
freeze_time
cooked = PrettyText.cook <<~MD
[date=2018-05-08 time=22:00 format=LLL timezones="Europe/Paris|America/Los_Angeles"]
MD
cooked_mail = <<~HTML
<p><span class="discourse-local-date" data-date="2018-05-08" data-format="LLL" data-timezones="Europe/Paris|America/Los_Angeles" data-time="22:00" data-email-preview="May 9, 2018 12:00 AM (Europe: Paris)">May 9, 2018 12:00 AM (Europe: Paris)</span></p>
HTML
end
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
context 'emails simplified rendering' do
it 'works with default markup' do
cooked = PrettyText.cook("[date=2018-05-08]")
cooked_mail = generate_html("2018-05-08T00:00:00Z (Etc: UTC)",
date: "2018-05-08",
email_preview: "2018-05-08T00:00:00Z (Etc: UTC)"
)
cooked = PrettyText.cook <<~MD
[date=2018-05-08 format=LLL timezone="Europe/Berlin" timezones="Europe/Paris|America/Los_Angeles"]
MD
cooked_mail = <<~HTML
<p><span class="discourse-local-date" data-date="2018-05-08" data-format="LLL" data-timezones="Europe/Paris|America/Los_Angeles" data-timezone="Europe/Berlin" data-email-preview="May 8, 2018 12:00 AM (Europe: Paris)">May 8, 2018 12:00 AM (Europe: Paris)</span></p>
HTML
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
end
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
it 'works with format' do
cooked = PrettyText.cook("[date=2018-05-08 format=LLLL]")
cooked_mail = generate_html("Tuesday, May 8, 2018 12:00 AM (Etc: UTC)",
date: "2018-05-08",
email_preview: "Tuesday, May 8, 2018 12:00 AM (Etc: UTC)",
format: "LLLL"
)
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
end
it 'works with time' do
cooked = PrettyText.cook("[date=2018-05-08 time=20:00:00]")
cooked_mail = generate_html("2018-05-08T20:00:00Z (Etc: UTC)",
date: "2018-05-08",
email_preview: "2018-05-08T20:00:00Z (Etc: UTC)",
time: "20:00:00"
)
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
end
end
end