import '../testHelper';
import CourseUtils from '../../app/assets/javascripts/utils/course_utils';

describe('CourseUtils.generateTempId', () => {
  it('creates a slug from term, title and school', () => {
    const course = {
      term: 'Fall 2015',
      school: 'University of Wikipedia',
      title: 'Introduction to Editing'
    };
    const slug = CourseUtils.generateTempId(course);
    expect(slug).to.eq('University_of_Wikipedia/Introduction_to_Editing_(Fall_2015)');
  });

  it('trims unnecessary whitespace', () => {
    const course = {
      term: ' Fall 2015',
      school: '   University of Wikipedia ',
      title: ' Introduction to Editing     '
    };
    const slug = CourseUtils.generateTempId(course);
    expect(slug).to.eq('University_of_Wikipedia/Introduction_to_Editing_(Fall_2015)');
  });
});

describe('CourseUtils.cleanupCourseSlugComponents', () =>
  it('trims whitespace from the slug-related fields of a course object', () => {
    const course = {
      term: ' Fall 2015',
      school: '   University of Wikipedia ',
      title: ' Introduction to Editing     '
    };
    CourseUtils.cleanupCourseSlugComponents(course);
    expect(course.term).to.eq('Fall 2015');
    expect(course.school).to.eq('University of Wikipedia');
    expect(course.title).to.eq('Introduction to Editing');
  })
);

describe('CourseUtils.i18n', () => {
  it('outputs an interface message based on a message key and prefix', () => {
    const message = CourseUtils.i18n('students', 'courses_generic');
    expect(message).to.eq('Editors');
  });

  it('defaults to the "courses" prefix if prefix is null', () => {
    const message = CourseUtils.i18n('students', null);
    expect(message).to.eq('Students');
  });

  it('takes an optional fallback prefix for if prefix is null', () => {
    const message = CourseUtils.i18n('class', null, 'revisions');
    expect(message).to.eq('Class');
  });
});

describe('CourseUtils.formatArticleTitle', () => {
  it('trims whitespace and replaces underscores', () => {
    const input = ' Robot_selfie  '
    const output = CourseUtils.formatArticleTitle(input)
    expect(output).to.eq('Robot selfie')
  });

  it('converts Wikipedia urls into titles', () => {
    const input = 'https://en.wikipedia.org/wiki/Robot_selfie'
    const output = CourseUtils.formatArticleTitle(input)
    expect(output).to.eq('Robot selfie')
  });

  it('handles url-encoded characters in Wikipedia urls', () => {
    const input = 'https://en.wikipedia.org/wiki/Jalape%C3%B1o'
    const output = CourseUtils.formatArticleTitle(input)
    expect(output).to.eq('Jalapeño')
  });
});

@@ -2055,16 +2055,17 @@
 elfie  '
+;
 %0A    con
@@ -2105,32 +2105,33 @@
 icleTitle(input)
+;
 %0A    expect(outp
@@ -2147,32 +2147,33 @@
 ('Robot selfie')
+;
 %0A  %7D);%0A%0A  it('co
@@ -2278,16 +2278,17 @@
 _selfie'
+;
 %0A    con
@@ -2328,32 +2328,33 @@
 icleTitle(input)
+;
 %0A    expect(outp
@@ -2378,16 +2378,17 @@
 selfie')
+;
 %0A  %7D);%0A%0A
@@ -2515,16 +2515,17 @@
 %25C3%25B1o'
+;
 %0A    con
@@ -2573,16 +2573,17 @@
 e(input)
+;
 %0A    exp
@@ -2611,16 +2611,17 @@
 lape%C3%B1o')
+;
 %0A  %7D);%0A%7D

Fix lint errors